Separate rows that contain certain string character
4 views (last 30 days)
Show older comments
Hi all,
I would like to separate the rows of the cellarray bellow in to two cellarrays, depending on the string character on the third column, whether it ends in 'B' or 'L'.
For example, i have the cellarray:
a={
'STING ONE' 40843 '09L';
'STRING TWO' 40842 '10B';
'STRING THREE' 40841 '11L';
'STRING FOUR' 40840 '12B';
'STRING FIVE' 40839 '03L'}
I would like to split the above automatically into:
b={
'STING ONE' 40843 '09L';
'STRING THREE' 40841 '11L';
'STRING FIVE' 40839 '03L'}
and
c={
'STRING TWO' 40842 '10B';
'STRING FOUR' 40840 '12B'}
would that be possible to do?
Thanks very much in advance for your help!
Kind regards,
Kosta.
0 Comments
Accepted Answer
Stephen23
on 10 Jan 2017
Edited: Stephen23
on 10 Jan 2017
>> idx = cellfun(@(s)'B'==s(end),a(:,3));
>> c = a(idx,:)
You could even put this in a function:
>> fun = @(c)a(cellfun(@(s)c==s(end),a(:,3)),:);
>> b = fun('L');
>> c = fun('B');
2 Comments
Stephen23
on 10 Jan 2017
A more efficient solution would be to run cellfun once to generate a character array of those characters:
>> vec = cellfun(@(s)s(end),a(:,3))
vec =
L
B
L
B
L
>> b = a(vec=='L',:);
>> c = a(vec=='B',:);
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!