In Excel you can use the "filter" function to find certain words in your columns. I want to do this in Matlab over the entire table.
Using the Matlab example-table "patients.dat" as example; my first idea was to use:
patients.Gender=={'Female'}
which does not work.
strcmp(patients.Gender,{'Female'})
workd only in one column ("Gender").
My problem: I have a table with different words say 'A','B','bananas','apples',.... spread out in an arbitrary manner in the columns of the table. I only want the rows that contain, say, 'A' and 'B'.
It is strange I did not find this in matlab "help" because it seems basic. I looked in stackedO but did not find an answer there either.
Here is a simpler, more elegant syntax for you:
Please note that in this example, you need to make sure that patients.Gender is a categorical type. You can use
categorical(variable)
to convert the variable to a categorical, and reassign it to the table variable like so:Here is a reference for you: https://www.mathworks.com/matlabcentral/answers/339274-how-to-filter-data-from-table-using-multiple-strings
A
table
in Matlab can be seen as an extendedcell array
. For example it additionally allows to name columns.However in your case you want to search in the whole
cell array
and do not care for any extra functionality of atable
. Therefore convert it withtable2cell
.Then you want to search for certain words. You could use a
regexp
but in the examples you mentionstrcmp
also is sufficient. Both work right away oncell arrays
.Finally you only need to
find
the rows of the logical search matrix.Here the example that gets you the rows of all patients that are 'Male' and in 'Excellent' conditions from the Matlab example data set:
which indeed prints out only male patients in excellent condition.