David Agban
David Agban

Reputation: 23

looping cell2mat to convert cell arrays to arrays

I have 4865 1-by-1 cell arrays, I need to convert them to into an ordinary array by cell2mat. When I run it I get the following error:

In an assignment A(I) = B, the number of elements in B and I must be the same.

for i=1:4865,

    c(i) = cell2mat(A(i))

end

Upvotes: 2

Views: 337

Answers (1)

Novice_Developer
Novice_Developer

Reputation: 1492

You cannot assign a whole matrix(which is in a cell) to one index

lets take the following example

>> cell_test ={[1 2 3;4 5 6],[1 2 3; 7 8 9]}

cell_test = 

    [2x3 double]    [2x3 double]

what you are doing is this

>> cell_test{1}

ans =

     1     2     3
     4     5     6

>> b(1) = cell_test{1}
Subscripted assignment dimension mismatch.

One of the options is that you create a new variable name for every new index through eval() like ofcourse there are many other options

>> i = 1

i =

     1

eval(['B_',num2str(i)  ,'=cell_test{i}'])

B_1 =

     1     2     3
     4     5     6

Update : structure method B(i).data = =cell_test{i}

Upvotes: 1

Related Questions