单元格数组删除每个单元格中的前导字符

时间:2015-09-15 10:03:09

标签: arrays string matlab cell-array

我有一个27 x 3的单元格,每个单元格都包含文本。

出于某些令人讨厌的原因,我正在使用的数据已经保存在每个以'开头的单元格中,我如何删除这些?

我的数据示例

   column 1    column 2   column 3
   'some       'text      'blah
   'more       'blah      'help

我想要什么

   column 1    column 2   column 3
   some       text      blah
   more       blah      help

使用cellfun的最佳方法是什么?如果是这样,你如何使用它?

2 个答案:

答案 0 :(得分:3)

cellfun与合适的anonymous function一起使用。匿名函数应该接受一个字符串并删除它的第一个字符,所以@(s) s(2:end)

c = {'''some' '''text' '''blah';
     '''more' '''blah' '''help'}; %// data
c = cellfun(@(s) s(2:end), c, 'uniformoutput', 0); %// remove first element of
                                                   %// each cell's contents

结果:

c = 
    'some'    'text'    'blah'
    'more'    'blah'    'help'

答案 1 :(得分:1)

另一种选择是使用regexprep

c = {'''some' '''text' '''blah';
     '''more' '''blah' '''help'}; %// data
c = regexprep(c, '''', '');

还会返回:

c = 

    'some'    'text'    'blah'
    'more'    'blah'    'help'