Remove rows from table when number in cell array is above certain value
8 views (last 30 days)
Show older comments
I would like to remove rows of a table when the number inside the text field of one column is greater than some number: For example:
test_table = table();
test_table.Var1 = {'A-01'; 'A-01'; 'A-01'; 'A-02'; 'A-02'; 'A-02'; 'A-03'; 'A-03'; 'A-03';};
test_table.Var2 = [1; 2; 3; 1; 2; 3; 1; 2; 3];
threshold = 2;
I would like to remove all rows with 'A-03' since 03 > threshold (in this case, 2).
While I found that extractAfter was useful for pulling off the numbers in the Var1 column, I'm having trouble converting these to a number array that I can compare logically to the Var2 column.
Thanks in advance for all the help!
0 Comments
Answers (1)
Peter Perkins
on 2 Apr 2019
I suggest you try using a categorical variable:
>> test_table.Var1 = categorical(test_table.Var1,{'A-01' 'A-02' 'A-03'},'Ordinal',true)
test_table =
9×2 table
Var1 Var2
____ ____
A-01 1
A-01 2
A-01 3
A-02 1
A-02 2
A-02 3
A-03 1
A-03 2
A-03 3
>> threshold = 'A-02';
>> test_table(test_table.Var1 > threshold,:)
ans =
3×2 table
Var1 Var2
____ ____
A-03 1
A-03 2
A-03 3
>> test_table(test_table.Var1 >= threshold,:)
ans =
6×2 table
Var1 Var2
____ ____
A-02 1
A-02 2
A-02 3
A-03 1
A-03 2
A-03 3
You might then say that I've just givewn you the opposite problem of the one you had before, but I bet it's easy to get from 2 to 'A-02' using sprintf.
0 Comments
See Also
Categories
Find more on Tables 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!