0

Similar to Excel, I need to find out how to filter out rows of a table that do not contain a certain string.

For example, I need only rows that contain the letters "MX". Within the sheet, there are rows with strings like ZMX01, MX002, and US001. I would want the first two rows.

This seems like a simple question, so I am surprised I couldn't find any help for this!

It is similar to the question Filter on words in Matlab tables (as in Excel)

Community
  • 1
  • 1
  • did you look at http://www.mathworks.com/help/stats/dataset-array-rows.html?requestedDomain=www.mathworks.com (I googled the search phrase "matlab select") – Tomer Levinboim Feb 08 '16 at 23:09
  • Are you filtering data from a text file, or have you loaded the data into the MATLAB workspace? – gariepy Feb 09 '16 at 03:25

1 Answers1

1

You may not find a lot of information on tables in MATLAB, as they were introduced with version R2013a, which is not that long ago. So, about your question: Let's first create a sample table:

% Create a sample table
col1 = {'ZMX01'; 'MX002'; 'US001'};
col2 = {5;7;3};
T = table(col1, col2);

T = 
     col1      col2
    _______    ____

    'ZMX01'    [5] 
    'MX002'    [7] 
    'US001'    [3] 

Now, MATLAB provides the rowfun function to apply any function to each row in a table. By default, the function you call has to be able to work on all columns of the table. To only apply rowfun to one column, you can use the 'InputVariables' parameter, which lets you specify either the number of the column (e.g. 2 for the second column) or the name of the column (e.g. 'myColumnName'). Then, you can set 'OutputFormat' to 'uniform' to get an array and not a new table as output.

In your case, you'll want to use strfind on the column 'col1'. The return value of strfind is either an empty array (if 'MX' wasn't found), or an array of all indices where 'MX' was found.

% Apply rowfun
idx = rowfun(@(x)strfind(x,'MX'), T, 'InputVariables', 'col1', 'OutputFormat', 'uniform');

The output of this will be

idx = 
    [2]
    [1]
    []

i.e. a 3-by-1 cell array, which is empty for 'US001' and contains a positive value for both other inputs. To create a subset of the table with this data, we can do the following:

% Create logical array, which is true for all rows to keep.
idx = ~cellfun(@isempty, idx);

% Save these rows and all columns of the table into a new table
R = T(idx,:);

And finally, we have our resulting table R:

R = 
     col1      col2
    _______    ____

    'ZMX01'    [5] 
    'MX002'    [7] 
hbaderts
  • 14,136
  • 4
  • 41
  • 48
  • Is the overhead of `rowfun` really necessary here? You obtain the same `idx` with `idx = strfind(T.col1, 'MX');` – sco1 Feb 09 '16 at 12:34
  • @excaza yes, you do get the same with `strfind`, so the overhead is not really necessary here. `rowfun` is especially useful for more complicated cases with multiple rows, or to directly create new columns with the results. – hbaderts Feb 11 '16 at 12:22