Remove rows from table when number in cell array is above certain value

5 次查看(过去 30 天)
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!

回答(1 个)

Peter Perkins
Peter Perkins 2019-4-2
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.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by