如何在MATLAB中作为函数调用实现单元数组扩展?

问题描述 投票:2回答:1

我有以下单元格数组:

>> tmp0 = {'foo', '%s', 'one'; 'bar', '%d', 3}

tmp0 =

  2×3 cell array

    'foo'    '%s'    'one'
    'bar'    '%d'    [  3]

我可以在sprintf中这样使用它:

>> sprintf('%s,%d', tmp0{:,3})

ans =

    'one,3'

我希望能够通过函数调用实现相同的功能,因为如果我有一个生成单元格数组的函数,例如genCell(),我认为我无法在MATLAB中实现类似genCell(){:}的功能。

所以我做了这个功能:

function cellExp(cellIn)
  cellIn{:}
end

尽管可疑,到目前为止似乎仍能按预期工作,因为调用cellExp(tmp0(:,3))似乎与调用tmp0{:,3}相同

>> cellExp(tmp0(:,3))

ans =

    'one'


ans =

     3


>> tmp0{:,3}

ans =

    'one'


ans =

     3

但是,最终,我不能按需要使用它:

>> sprintf('%s,%d', cellExp(tmp(:,3)))
Error using cellExp
Too many output arguments.
matlab cell-array
1个回答
1
投票

您收到的最后一条错误消息是因为cellExp(tmp0(:,3))的输出是comma-separeted list

© www.soinside.com 2019 - 2024. All rights reserved.