read cell array and split into one cell array of multiple rows
3 views (last 30 days)
Show older comments
Hello,
I have a cell array with strings, for example:
names =
2×1 cell array
{'A B C D E F G H '}
{'I J ' }
I want them in an array like this one:
KNAME =
10×1 cell array
{'A'}
{'B'}
{'C'}
{'D'}
{'E'}
{'F'}
{'G'}
{'H'}
{'I'}
{'J'}
I do something too complicated that works but I am guessing there is an easier solution to this
0 Comments
Accepted Answer
Stephen23
on 29 Jan 2019
Edited: Stephen23
on 29 Jan 2019
name = {...
'ABCDEFGHIJKLMNOPB C D E F G H';...
'I J '};
kname = regexp(name,'\w{1,16}','match');
kname = [kname{:}].'
Giving:
>> kname{:}
ans = ABCDEFGHIJKLMNOP
ans = B
ans = C
ans = D
ans = E
ans = F
ans = G
ans = H
ans = I
ans = J
3 Comments
Stephen23
on 29 Jan 2019
Edited: Stephen23
on 29 Jan 2019
To include a period character you can use this regular expression:
'[\w\.]{1,16}'
That will match from 1 to 16 letters, digits, undescores, and periods. The MATLAB documentation explains how regular expressions are defined:
It does take a lot of practice and reading the documentation to get used to regular expressions, so feel free to ask if you want more help.
More Answers (1)
Omer Yasin Birey
on 29 Jan 2019
name{1,:} = {'A B C D E F G H'};
name{2,:} = {'J '};
str = string(name);
C = arrayfun( @(x) strsplit( x, ' ' ), str, 'UniformOutput', false );
conc = horzcat(C{:})';
cellA = cellstr(conc);
2 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!