在MATLAB中将类数组的元素分配给各个变量的问题

时间:2010-06-14 14:42:11

标签: arrays matlab variables variable-assignment

这与this questionthis questionthis question有点重复,但这些解决方案不起作用,所以我问我的。

我有一组本地定义的类,我想将它分配给多个单独的变量。这种模式不起作用:

%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.

因为temp是一个单元格,而我的2x1数组在一个单元格中,而不是一个2x1单元格数组,每个单元格中包含一个元素。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您应该使用函数NUM2CELL而不是函数MAT2CELL,以便将数组a的每个元素放在单元格数组temp的单独单元格中。

仅使用一个输入使用MAT2CELL相当于执行temp = {a};,而在我的MATLAB版本(R2009a)中,我实际上会收到此警告:

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