在MATLAB中将单元格的单元格数组转换为字符串的单元格数组
在字符串的单元格数组上使用带标记的正则表达式,我得到了单元格的单元格数组.这是简化的示例:
Using regexp with tokens on cell array of strings I've got cell array of cells. Here is simplified example:
S = {'string 1';'string 2';'string 3'};
res = regexp(S,'(\d)','tokens')
res =
{1x1 cell}
{1x1 cell}
{1x1 cell}
res{2}{1}
ans =
'2'
我知道S中每个单元格字符串只有一个匹配项.如何将该输出转换为矢量化形式的字符串单元格数组?
I know I have only one match per cell string in S. How I can convert this output into cell arrays of strings in a vectorized form?
问题比您想象的还要严重.您来自 REGEXP 的输出实际上是一个单元格单元格数组单元格数组的个数组的字符串!是的,三个级别!以下使用 CELLFUN 摆脱前两个级别,只剩下一个单元格的字符串数组:
The problem is even worse than you thought. Your output from REGEXP is actually a cell array of cell arrays of cell arrays of strings! Yeah, three levels! The following uses CELLFUN to get rid of the top two levels, leaving just a cell array of strings:
cellArrayOfStrings = cellfun(@(c) c{1},res);
但是,您也可以将呼叫更改为 REGEXP 摆脱一个级别,然后使用 VERTCAT :
However, you can also change your call to REGEXP to get rid of one level, and then use VERTCAT:
res = regexp(S,'(\d)','tokens','once'); %# Added the 'once' option
cellArrayOfStrings = vertcat(res{:});