在MATLAB中将单元格的单元格数组转换为字符串的单元格数组 [英] Convert cell array of cells into cell array of strings in MATLAB

查看:144
本文介绍了在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{:});

这篇关于在MATLAB中将单元格的单元格数组转换为字符串的单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆