Home Ask Login Register

Developers Planet

Your answer is one click away!

Trey Torno February 2016

Filter on parts of words in Matlab tables

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)


hbaderts February 2016

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 = 

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 row 

Post Status

Asked in February 2016
Viewed 3,631 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life