how can I write a function

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?

 Accepted Answer

Dave B
Dave B on 17 Nov 2021
Edited: Dave B on 17 Nov 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
ans = 47×1 cell array
{'Smith' } {'Johnson' } {'Wilson' } {'Moore' } {'Jackson' } {'White' } {'Martin' } {'Thompson' } {'Martinez' } {'Robinson' } {'Hall' } {'Hernandez'} {'King' } {'Scott' } {'Green' } {'Baker' } {'Nelson' } {'Mitchell' } {'Perez' } {'Roberts' } {'Turner' } {'Phillips' } {'Parker' } {'Edwards' } {'Collins' } {'Stewart' } {'Reed' } {'Bell' } {'Murphy' } {'Ward' }
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")
ans = 47×1 string array
"Smith" "Johnson" "Wilson" "Moore" "Jackson" "White" "Martin" "Thompson" "Martinez" "Robinson" "Hall" "Hernandez" "King" "Scott" "Green" "Baker" "Nelson" "Mitchell" "Perez" "Roberts" "Turner" "Phillips" "Parker" "Edwards" "Collins" "Stewart" "Reed" "Bell" "Murphy" "Ward"

5 Comments

I can use first option but if the postion of gender is changed in certain database. like
'gender''name' 'age'
'f' 'mary' 25
'm' 'john' 35
'f' 'anna' 30
'm' 'paul' 22
'f' 'elaina' 38
using data(:,2) can not be used. how can i generlize that?
if i have to use this for a certain age like between 30 and 40
this what i have done so far
function [patient] = filterpatients_cell(data)
patient = data(strcmp(data(:,2),'f'),1) && data(strcmp(data(:,3),'age'=< 30 && 'age' >=40),1);
Re how to generalize, this is why using a table makes more sense, because then you can refer to a variable name instead of a number. However you could look at the first row with the same principal
load patients
data=[{'name'} {'gender'} {'age'}; LastName Gender num2cell(Age)];
col = strcmp(data(1,:),'gender');
data(strcmp(data(:,col),'Male'),1)
ans = 47×1 cell array
{'Smith' } {'Johnson' } {'Wilson' } {'Moore' } {'Jackson' } {'White' } {'Martin' } {'Thompson' } {'Martinez' } {'Robinson' } {'Hall' } {'Hernandez'} {'King' } {'Scott' } {'Green' } {'Baker' } {'Nelson' } {'Mitchell' } {'Perez' } {'Roberts' } {'Turner' } {'Phillips' } {'Parker' } {'Edwards' } {'Collins' } {'Stewart' } {'Reed' } {'Bell' } {'Murphy' } {'Ward' }
Re how to apply to age
  • this approach won't work, 'age' can't be compared to 30.
  • strcmp just compares strings, it's not useful for numbers.
  • =< is not a matlab operation
  • If age a cell, you'll have to convert it to a matrix, and then it'll be off by one row because of the headings.
  • Really just use a table, your life will be much easier!
with this code im getting an emply cell.
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).

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Tags

Asked:

on 17 Nov 2021

Commented:

on 18 Nov 2021

Community Treasure Hunt

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

Start Hunting!