Filter a table by 2 columns
Ältere Kommentare anzeigen
Hi, I'm using 'patients' (load patients) to learn more about tables. I'm trying to filter the table by gender and smoker status by following
load patients
T = table(LastName,Age,Smoker,Gender)
Tsmokermale=(T.Smoker==true| T.Gender=='Male');
but get the error
Undefined operator '==' for input arguments of type 'cell'.
How can I filter the table by male and smoker, and still keep the information of LastName and Age?
2 Kommentare
madhan ravi
am 19 Feb. 2019
please show how you are forming a table
Orongo
am 27 Feb. 2019
Antworten (2)
Peter Perkins
am 11 Mär. 2019
Orongo, do this:
T.Gender = categorical(T.Gender);
Then this
Tsmokermale=(T.Smoker==true| T.Gender=='Male');
works as you expect, and what you are really asking about is this
T(Tsmokermale,:)
Long story short: Gender in the mat file is what's called a cell array of char vectors (a.k.a. a "cellstr"). Those don't support testing using ==, thus KSSV's suggestion of strcmp. But if you convert them to categorical, you can use ==. You could also convert to string, but categorical is more appropriate in this case.
1 Kommentar
Ishwor Koirala
am 21 Dez. 2022
That worked for me. Thank you very much :)
KSSV
am 19 Feb. 2019
idx = strcmp(T.Gender,'Male') ;
1 Kommentar
Orongo
am 27 Feb. 2019
Bearbeitet: madhan ravi
am 27 Feb. 2019
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!