MATLAB: Find matched string in table

MATLABtable

Hi,
To find data matching certain conditions in a table we use:
rows = (T.Smoker==true & T.Age<40);
What if the T.Smoker field was not a logical but a string? 'yes' or 'no'.
rows = (T.Smoker=='yes' & T.Age<40);
This later code does not work. How could I make it work so the condition matches a certain string?
Thank you,
TD

Best Answer

Compare the text data stored in the table with a string array. Let's build a sample using the example from the documentation for the table function.
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
Let's extract patients under 40, separated by gender.
malePatientsUnder40 = patients(patients.Gender == "Male" & patients.Age < 40, :)
femalePatientsUnder40 = patients(patients.Gender == "Female" & patients.Age < 40, :)
Let's check against the full list of patients under 40.
patientsUnder40 = patients(patients.Age < 40, :);
Combine the male and female patients into one larger table and compare it against the full list.
isequal(sortrows([malePatientsUnder40; femalePatientsUnder40]), sortrows(patientsUnder40))