2

I am relatively new to matlab, and I was wondering if there was a simpler way to do the following:

Given mycellarray = {[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]}, I would like to assign each matrix inside mycellarray to a separate variable. Is there any faster/better/shorter way to do it than this?

a = cell2mat(mycellarray(1,1))
b = cell2mat(mycellarray(1,2))
c = cell2mat(mycellarray(1,3))
d = cell2mat(mycellarray(1,4))

Thanks in advance!

2 Answers 2

4

[a,b,c,d]=mycellarray{:}

The {:} makes a comma seperated list of the cell array, which an be assigned to individual variables.

relevant documentation pages:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I always figured that I was spending way more time than was necessary.
1

Here a short example on how to do what you want with the MATLAB function deal.

a = {[1 2 3] [4 5 6] [7 8 9]}
[aa bb cc] = deal(a{:})

3 Comments

This appears to answer the question, but it could use a little more explanation.
This is currently in the Low Quality Posts queue. I'm voting not to delete, but it needs to be updated with some explanation.
This does answer the question, but deals (no pun intended) with a rather rare function: deal. Please update your post with an explanation of how this works or it will inevitably be removed as it's in the Low Quality queue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.