【问题标题】:Convert numbers to strings in a cell array in MATLAB在 MATLAB 中将数字转换为元胞数组中的字符串
【发布时间】:2013-02-25 21:45:02
【问题描述】:

我有一个包含数字和字符串数据的元胞数组。我需要将数字转换为字符串,以便可以使用unique() 函数。

a = {1; 4; 'lf'}
result --> {'1', '4', 'lf'}; % Now unique() function can be used

有在线解决方案可以处理列为数字的情况。但是这些不能在这里使用,因为至少有 1 行有字符串作为数据。应赞赏矢量化解决方案。

【问题讨论】:

    标签: string matlab type-conversion cell-array


    【解决方案1】:

    使用cellfun()num2str() 应用于每个单元格元素:

    result = cellfun(@num2str, a, 'UniformOutput', false)
    

    这(将UniformOutput 设置为false)将自动处理数组的非标量字符元素。

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多