【问题标题】:Problem with assigning elements of a class array to individual variables in MATLAB将类数组的元素分配给MATLAB中的各个变量的问题
【发布时间】:2011-03-03 13:29:43
【问题描述】:

这有点重复 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 单元格数组,其中每个原始数组的一个元素位于一个单元格中。

有什么想法吗?

【问题讨论】:

    标签: arrays matlab variables variable-assignment


    【解决方案1】:

    您应该使用函数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
    

    【讨论】:

    • 谢谢。由于 mat2cell 失败,我没有费心去看 num2cell。弗拉金的数学作品。文档说:“C = num2cell(A) 将数值数组 A 转换为元胞数组 C”,然后继续说:“num2cell 适用于所有数组类型。”
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2016-06-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多