Convert an array of cell cells to an array of row cells in MATLAB

Using regexp with tokens in an array of row cells. I got an array of cell cells. Here is a 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' 

I know that I have only one match for a cell row in S. How can I convert this output to arrays of row cells in vector form?

+6
string regex matlab cell
source share
1 answer

The problem is even worse than you thought. Your output from REGEXP is actually an array of cells of arrays of cells of arrays of cells of rows! Yes, three levels! The following actions use CELLFUN to get rid of the top two levels, leaving only an array of row cells:

 cellArrayOfStrings = cellfun(@(c) c{1},res); 

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{:}); 
+12
source share

All Articles