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]