remove rows in table based on criteria across two columns

15 views (last 30 days)
How do I remove rows based on whether or not a substring in one column matches with a number in a second column according to a pre-defined criteria?
The criteria;
80ms,140ms & 1
100ms,250ms & 2
250ms,600ms & 3
600ms,900ms & 4
An example of the data below. I want to delete the rows below where 250ms,600ms is in the first column and the number in the second column is a number other than 3, and delete rows where 100ms,250ms is in the first column and a number other than 2 is in the second column.
'CG_11_21_250ms,600ms_CPR' 1
'CG_11_21_250ms,600ms_CPR' 2
'CG_11_21_250ms,600ms_CPR' 3
'CG_11_21_250ms,600ms_CPR' 4
'CG_12_22_100ms,250ms_FCL' 1
'CG_12_22_100ms,250ms_FCL' 2
'CG_12_22_100ms,250ms_FCL' 3
'CG_12_22_100ms,250ms_FCL' 4

Accepted Answer

Turlough Hughes
Turlough Hughes on 3 Nov 2021
Edited: Turlough Hughes on 3 Nov 2021
Try the following:
T(contains(string(T{:,1}),'80ms,140ms') & T{:,2} ~= 1,:) = [];
T(contains(string(T{:,1}),'100ms,250ms') & T{:,2} ~= 2,:) = [];
T(contains(string(T{:,1}),'250ms,600ms') & T{:,2} ~= 3,:) = [];
T(contains(string(T{:,1}),'600ms,900ms') & T{:,2} ~= 4,:) = [];
  3 Comments
DavidL88
DavidL88 on 5 Nov 2021
Hi Turlough,
Could you advise how I can adapt your code to this table to delete according to two strings? In the adaptation below I outline the two conditions to remove the row but I get this 'Operator '&' is not supported for operands of type 'string'.'
T(contains(string(T{:,2}),'11_right') & string(T{:,3}),'earlyP3') = [];
T(contains(string(T{:,2}),'21_right') & string(T{:,3}),'earlyP3') = [];

Sign in to comment.

More Answers (1)

DavidL88
DavidL88 on 3 Nov 2021
Thanks for your reply. I'm getting this error?
Error using contains
First argument must be a string array, character vector, or cell array of character vectors.
  3 Comments

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!