WithoutAnAce
WithoutAnAce

Reputation: 55

How do I assign a cell array to multiple matrices?

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!

Upvotes: 2

Views: 70

Answers (2)

m_power
m_power

Reputation: 3194

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{:})

Upvotes: 1

Daniel
Daniel

Reputation: 36710

[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:

Upvotes: 4

Related Questions