How to do double strings match in single statement

2 views (last 30 days)
Hi, I have below cell array matrix and I want check two strings matchig in single statement:
condition: If row element in column1 is Normal & column2 value>=20, then copy to normalData, else copy to abnormalData InputData:
Re/Attempt 12
Normal 24
Normal 13
Re/Attempt 26
Re/Attempt 28
Normal 23
Normal 12
Normal 15
normalData:
Normal 24
Normal 23
abnormalData:
Re/Attempt 12
Normal 13
Re/Attempt 26
Re/Attempt 28
Normal 12
Normal 15
Many thanks in advance,

Answers (2)

Stephen23
Stephen23 on 28 Apr 2017
Edited: Stephen23 on 28 Apr 2017
C = {...
'Re/Attempt','12';...
'Normal','24';...
'Normal','13';...
'Re/Attempt','26';...
'Re/Attempt','28';...
'Normal','23';...
'Normal','12';...
'Normal','15'};
idx = strcmp('Normal',C(:,1)) & str2double(C(:,2))>20;
normalData = C(idx,:)
abnormalData = C(~idx,:)
  1 Comment
KL
KL on 28 Apr 2017
I was writing something similar but yours is neat and clean, voted!

Sign in to comment.


dpb
dpb on 28 Apr 2017
Edited: dpb on 28 Apr 2017
If you use a cell array, that'll require cellfun
> isOK=~cellfun(@isempty,strfind(C(:,1),'Normal')) & [[C{:,2}]>=20].';
>> C(isOK,:)
ans =
'Normal' [24]
'Normal' [23]
>>
Other is just the complement of ~isOK of course.
This would be far easier to write if you would use a table and categorical variables.
NB: here I presumed the second cell is numeric not string...if were another string have to "wash, rinse, repeat" the strfind operation for each.
regexp is a little more friendly with cell strings but as noted, the table class and categorical variables are very useful for such data.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!