Find matched string in table

조회 수: 64 (최근 30일)
The Finnish Rein Deer
The Finnish Rein Deer 2020년 3월 9일
댓글: The Finnish Rein Deer 2020년 3월 11일
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

채택된 답변

Steven Lord
Steven Lord 2020년 3월 9일
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))
  댓글 수: 4
Steven Lord
Steven Lord 2020년 3월 10일
If you build the patients table using the patients.mat file the Gender variable is actually a cellstr, a cell array containing char vectors. It's not a string array, though you could use the cellstr to make one.
Comparing char arrays with string or categorical arrays we try to do "the nice thing" and and treat the text more like "words" rather than "bunches of characters". See this documentation page for a discussion about comparing char and string arrays and this one for char and categorical.
How would these work? Let's rebuild the patients table.
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
What type is the Gender variable in patients? It's a cellstr.
class(patients.Gender)
iscellstr(patients.Gender)
Since in this data set Gender only takes two values, we can store and display them as two categories using a categorical array. We can match a categorical array using a char vector, a string, or a categorical value. [I'm switching the Age threshold to 30 just so the filtered table arrays are shorter.]
patients.GenderCat = categorical(patients.Gender);
F1 = patients(patients.GenderCat == 'Female' & patients.Age < 30, :)
F2 = patients(patients.GenderCat == "Female" & patients.Age < 30, :)
FCategory = patients.GenderCat(3) % The first Female patient is in row 3
F3 = patients(patients.GenderCat == FCategory & patients.Age < 30, :)
Or you can turn that cellstr data into string.
patients.GenderStr = string(patients.Gender);
F4 = patients(patients.GenderStr == "Female" & patients.Age < 30, :)
F5 = patients(patients.GenderStr == 'Female' & patients.Age < 30, :)
The Finnish Rein Deer
The Finnish Rein Deer 2020년 3월 11일
Thank you so much! All clear.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2020년 3월 9일
isequal(T.Smoker, 'yes')
strcmpi(T.Smoker, 'yes')
  댓글 수: 1
The Finnish Rein Deer
The Finnish Rein Deer 2020년 3월 10일
Thanks. Isequal does not work, and strcmpi is better to replace for strcmp, since the former would match independently of case.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 3월 9일
Try contains:
rows = contains(T.Smoker, 'yes') & T.Age<40;
  댓글 수: 1
The Finnish Rein Deer
The Finnish Rein Deer 2020년 3월 10일
Actually, I realized that the code I suggested would work if only I had changed simple quotes for double ones!
rows = (T.Smoker=='yes' & T.Age<40); % does not work
rows = (T.Smoker=="yes" & T.Age<40); % does work

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by