登录论坛

查看完整版本 : 在MATLAB中将类数组的元素分配给各个变量的问题


poster
2019-12-10, 20:41
这是该问题 (https://stackoverflow.com/questions/2893356/matlab-easiest-way-to-assign-elements-of-a-vector-to-individual-variables) , 该问题 (https://stackoverflow.com/questions/2337126/multiple-assignment-in-matlab)和该问题 (https://stackoverflow.com/questions/2740704/is-there-anything-like-deal-for-normal-matlab-arrays)的重复部分,但是这些解决方案不起作用,所以我要问我。

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

%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单元格数组,而每个原始数组中的每个元素只有一个元素。

有任何想法吗?



回答:

您应该使用函数NUM2CELL (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2cell.html)而不是函数MAT2CELL (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mat2cell.html) ,以便将数组a每个元素放置在单元格数组temp的单独单元格中。

仅使用一个输入使用MAT2CELL (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mat2cell.html)等同于执行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

更多&回答... (https://stackoverflow.com/questions/3038152)