How to get all the individuals in a table with a certain string value

15 views (last 30 days)
If i have a table with some variables A,B,C... i would like to know how to get all the individuals that have the same string value por a variable. For example, lets say A is Gender; i would like to know how to get all the rows that have 'Male' or 'Female' on the Gender column.

Answers (1)

Star Strider
Star Strider on 6 Dec 2021
See if the findgroups function will produce the desired result.
.
  5 Comments
Star Strider
Star Strider on 6 Dec 2021
My pleasure!
Another option would be the unique function, however findgroups is a bit more robust.
If my Answer helped you solve your problem, please Accept it!
.
Steven Lord
Steven Lord on 6 Dec 2021
You could also use string or categorical operations.
load patients
T = table(LastName, Gender, Smoker);
T.Gender = categorical(T.Gender); % Change from cellstr to categorical
head(T)
ans = 8×3 table
LastName Gender Smoker ____________ ______ ______ {'Smith' } Male true {'Johnson' } Male false {'Williams'} Female false {'Jones' } Female false {'Brown' } Female false {'Davis' } Female false {'Miller' } Female true {'Wilson' } Male false
allMales = T(T.Gender == 'Male', :) % 'Male' converted to categorical for comparison
allMales = 47×3 table
LastName Gender Smoker _____________ ______ ______ {'Smith' } Male true {'Johnson' } Male false {'Wilson' } Male false {'Moore' } Male false {'Jackson' } Male false {'White' } Male true {'Martin' } Male true {'Thompson' } Male true {'Martinez' } Male false {'Robinson' } Male false {'Hall' } Male false {'Hernandez'} Male false {'King' } Male true {'Scott' } Male false {'Green' } Male false {'Baker' } Male true
allSmiths = T(T.LastName == "Smith", :) % "Smith" is a string
allSmiths = 1×3 table
LastName Gender Smoker _________ ______ ______ {'Smith'} Male true
allStartsWithW = T(startsWith(T.LastName, "W"), :) % Can perform other string ops
allStartsWithW = 9×3 table
LastName Gender Smoker ______________ ______ ______ {'Williams' } Female false {'Wilson' } Male false {'White' } Male true {'Walker' } Female true {'Wright' } Female true {'Ward' } Male false {'Watson' } Female true {'Wood' } Male false {'Washington'} Female false

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!