MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中将单元格的单元格数组转换为字符串的单元格数组 (https://www.labfans.com/bbs/showthread.php?t=23306)

poster 2019-12-10 20:30

在MATLAB中将单元格的单元格数组转换为字符串的单元格数组
 
在字符串的单元格数组上使用带有标记的regexp,我得到了单元格的单元格数组。这是简化的示例:

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中每个单元格字符串只有一个匹配项。如何将该输出转换为矢量化形式的字符串单元格数组?



[B]回答:[/B]

这个问题比你想的还要严重。您从[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html"]REGEXP的[/URL]输出实际上是一个字符串[I]的单元格数组[/I]的一个单元格数组的一个单元[I]格数组[/I] !是的,三个级别!以下使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN[/URL]摆脱了前两个级别,只剩下一个单元格字符串数组:

cellArrayOfStrings = cellfun(@(c) c{1},res); 但是,您也可以[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/vertcat.html"]将对[/URL] [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html"]REGEXP[/URL]的调用更改为摆脱一个级别,然后使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/vertcat.html"]VERTCAT[/URL] :

res = regexp(S,'(\d)','tokens','once'); %# Added the 'once' option cellArrayOfStrings = vertcat(res{:});

[url=https://stackoverflow.com/questions/2624744]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 04:49

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.