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'
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?
str = string( A ) converts the input array to a string array. For instance, if A is numeric vector [1 20 300] , str is a string array of the same size, ["1" "20" "300"] . str = string( A , dateFmt ) , where A is a datetime or duration array, applies the specified format, such as "HH:mm:ss" .
A = cell2mat( C ) converts a cell array into an ordinary array. The elements of the cell array must all contain the same data type, and the resulting array is of that data type. The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined.
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);
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{:});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With