how can I write a function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manav Divekar
el 17 de Nov. de 2021
Comentada: Dave B
el 18 de Nov. de 2021
i have a data base with name gender and age not necessarily in the same order, some data base is gender name age. for example
'name' 'gender' 'age'
'mary' 'f' 25
'john' 'm' 35
'anna' 'f' 30
'paul' 'm' 22
'elaina' 'f' 38
if i have to use this database as cell array and only get the names of male how i do so?
0 comentarios
Respuesta aceptada
Dave B
el 17 de Nov. de 2021
Editada: Dave B
el 17 de Nov. de 2021
Your data look similar to this (as you've described them):
load patients
data=[{'name'} {'gender'} {'age'}; LastName Gender num2cell(Age)]
You just use strcmp to compare strings, and specify you want the rows where gender contains 'm' and column 1
data(strcmp(data(:,2),'Male'),1) % obviously you'll use 'm' in your case
If it were me, I'd put these in a table and use strings, it's so much better!
t=array2table(data(2:end,:),'VariableNames',data(1,:));
t.name=string(t.name);
t.gender=categorical(t.gender);
t.age=cell2mat(t.age);
t.name(t.gender=="Male")
5 comentarios
Dave B
el 18 de Nov. de 2021
Sorry i've updated the code, and added a demo (but really my intent ws to give you a strategy you can use rather than to write the code for you).
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!