3
votes

How can I easily filter a table in Matlab? Let's say I want to filter a table and only keep rows where the value in column 5 is larger than 30. How does this work?

1

1 Answers

5
votes

You can do this just like you would normal logical indexing.

tbl = table(rand(10,1), rand(10,1), 'VariableNames', {'a', 'b'});

     a          b   
  _______    _______

  0.64432    0.20774
  0.37861    0.30125
  0.81158    0.47092
  0.53283    0.23049
  0.35073    0.84431
    0.939    0.19476
  0.87594    0.22592
  0.55016    0.17071
  0.62248    0.22766
  0.58704     0.4357

Now we grab all rows where column a is greater than 0.5 (tbl.a > 0.5)

tbl(tbl.a > 0.5,:)

     a          b   
  _______    _______

  0.64432    0.20774
  0.81158    0.47092
  0.53283    0.23049
    0.939    0.19476
  0.87594    0.22592
  0.55016    0.17071
  0.62248    0.22766
  0.58704     0.4357

You can also access it using the column index.

tbl(tbl{:,1} > 0.5,:)

For your case this would be tbl(tbl{:,5} > 30,:)

More information about accessing table data here.