views:

58

answers:

1

This is a bit of a duplicate of this question, this question, and this question, however those solutions don't work, so I'm asking mine.

I've got an array of locally defined classes and I'd like to assign it to multiple, individual variables. This pattern doesn't work:

%a is 2x1 of MyClass
temp = mat2cell(a);
[x,y] = temp{:};

%throws:
??? Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

Because temp is a single cell, with my 2x1 array in one cell, rather than a 2x1 cell array with one element of each of my original array in one cell.

Any ideas?

+3  A: 

You should use the function NUM2CELL instead of the function MAT2CELL in order to place each element of your array a in a separate cell of your cell array temp.

Using MAT2CELL with just one input is equivalent to doing temp = {a};, and in my version of MATLAB (R2009a) I actually get this warning:

>> temp = mat2cell(a);
Warning: Single input behavior is obsolete and will be removed in a
         future release of MATLAB. Use C={X} instead. 
> In mat2cell at 54
gnovice
Thanks. With mat2cell failing, I didn't bother to look at num2cell. Frakin' mathworks. Docs say: "C = num2cell(A) converts numeric array A into cell array C ", but then continue on to say: "num2cell works for all array types."
Marc