【问题标题】:how to convert a cell to string in matlabmatlab如何将单元格转为字符串
【发布时间】:2012-11-06 20:03:19
【问题描述】:

假设我有一个单元格

v =    'v'    [576.5818]    [3.0286]    [576.9270]

       'v'    [576.5953]    [3.1180]    [576.8716]

       'f'    [      56]    [    58]    [      52]

       'f'    [      56]    [    58]    [      52]

我想使用每个元素的格式字符串将其转换为元胞数组:' %.5f'

我该怎么做?我尝试了以下方法,但出现错误:

f1 = @(x) sprintf('   %.5f',x);
cellfun(f1, num2cell(v),'UniformOutput', false) 

我收到一个错误 ???

Error using ==> sprintf

Function is not defined for 'cell' inputs.

Error in ==> @(x)sprintf(' %.5f',x)

谁能帮帮我提前谢谢

【问题讨论】:

    标签: string matlab cell


    【解决方案1】:

    字符串是元胞数组

    嗯,不是真的.. 这是一个矩阵,但请继续阅读。

    我猜元胞数组是 MATLAB 中最神秘的数据类型。所以让我们揭开神秘面纱 ;-)

    假设

    fruits = {...
        'banana',...
        'apple',...
        'orange'...
    }
    

    首先,小数组不需要整数索引。最好使用类似 foreach 的结构。确实,

    for index = 1:numel(fruits)
        fruits{index}
    end
    

    等价于

    for fruit = fruits
        fruit
    end
    

    对吗?

    嗯,不完全是。第一个循环产生字符串,而第二个循环产生单元格。你可以检查一下

    for index = 1:numel(fruits)
        [isstr(fruits{index}) iscell(fruits{index})]
    end
    
    for fruit = fruits
        [isstr(fruit) iscell(fruit)]
    end
    

    ,即[1 0][0 1]

    如果您发现了差异,那么您必须知道如何处理下一个示例(在这个示例中确实与您的问题有关 (!) 我保证!)。假设您尝试在循环中进行水平连接:

    for fruit = fruits
        [fruit 'is a fruit']
    end
    

    你会得到

    ans = 
    
        'banana'    'is a fruit'
    

    等等。为什么?显然,此代码尝试将嵌套单元格数组连接到字符串(包含字符矩阵的单元格数组,这些字符构成字符串,如“香蕉”)。所以,正确答案是

    使用 {:}

    for fruit = fruits
        [fruit{:} 'is a fruit']
    end
    

    神奇的是,这已经产生了预期的'香蕉是水果''苹果是水果',等等。

    提示

    一些提示:

    • 无索引循环与 for fruit = [fieldnames][1](fruits)' 中的结构很好地配合使用
    • 以上是 true 对于开源 octave
    • 香蕉不仅仅是水果,在分类学上它也是一种草本植物 ;-) 就像 MATLAB 中的 'banana' 既是字符串又是矩阵,即 assert(isstr('banana') && ismat('banana'))通过,但 assert(iscell('banana')) 失败。
    • {:} 等价于 cell2mat

    PS

    您的问题的解决方案可能如下所示:

    给定

    vcell = {...
        'v'    576.5818    3.0286  576.9270;
        'v'    576.5818    3.0286  576.9270    
    }
    

    将仅按索引的数字类型转换为字符串

     vcell(cellfun(@isnumeric, vcell)) =  cellfun(@(x) sprintf('%.5f', x), vcell(cellfun(@isnumeric, vcell)), 'UniformOutput', false)
    

    以上代码输出

    vcell =

    'v'    '576.58180'    '3.02860'    '576.92700'
    'v'    '576.58180'    '3.02860'    '576.92700'
    

    可以串联。

    【讨论】:

      【解决方案2】:

      假设我们有一个单元格如下:

      my_cell = {'Hello World'}  
      class(my_cell)
      ans = 
      cell
      

      我们可以直接使用{:} 操作符来取出字符串。

         class(my_cell{:})
          ans =
          char
      

      请注意,我们可以在任何可以使用普通字符串的地方使用表达式 mycell{:}

      【讨论】:

      • 干得好!永远不要说掘墓对任何人没有帮助。
      • 调用多余的函数调用sprintf hack - 太棒了
      【解决方案3】:

      试试这个:

      sprintf('   %.5f',x{:})
      

      (根据一些Google results工作。)

      【讨论】:

        【解决方案4】:

        通过查看 strjoin.m 文件,我发现了这个:

        string = [x{:}];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-28
          • 1970-01-01
          相关资源
          最近更新 更多