what wrong I am doing in the code?

i have to filter out female above age 30 and less than 40 fron the give data base
'name', {'mary','john','anna','paul','elaina'},
'gender',{'f', 'm', 'f', 'm', 'f'},
'age' ,{25, 35, 30, 22, 38}
Using structure method
function [patient] = filterpatients_struct(data)
t=array2table(data(2:end,:),'gender',data(1,:));
t.name=string(t.name);
t.gender=categorical(t.gender);
t.age=cell2mat(t.age);
t.name(t.gender=="f");
patient = t.name(t.gender=="f");

답변 (1개)

KSSV
KSSV 2021년 11월 18일

1 개 추천

% Make required structure
S = struct ;
S.name = {'mary','john','anna','paul','elaina'} ;
S.gender = {'f', 'm', 'f', 'm', 'f'} ;
S.age = [25, 35, 30, 22, 38] ;
idx = strcmp(S.gender,'f') ; % Indices of females
iwant = S.age(S.age(idx) >=30 & S.age(idx) <= 40 ) % age criteria

댓글 수: 7

Manav Divekar
Manav Divekar 2021년 11월 18일
편집: Manav Divekar 2021년 11월 18일
this will only work if i manually input the values. i want to call a function so that if i ask in cammand window to display.
function [patient] = filterpatients_struct(data)
S.name = strcmp(data(1,:),'name');
S.gender = data(strcmp(data(2,:),'gender'),1);
S.age = data(strcmp(data(3,:),'age'),1);
idx = strcmp(S.gender,'f') ; % Indices of females
iwant = S.age(S.age(idx) >=30 & S.age(idx) <= 40 ); % age criteria
patient = iwant;
KSSV
KSSV 2021년 11월 18일
You can write a function for that..not a big deal.
Manav Divekar
Manav Divekar 2021년 11월 18일
this is not working i tried
KSSV
KSSV 2021년 11월 18일
Why it will not work? What input you want to give to function and what you are expecting output?
Manav Divekar
Manav Divekar 2021년 11월 18일
input
disp(filterpatients_struct( struct( ...
'name', {'mary','john','anna','paul','elaina'}, ...
'gender',{'f', 'm', 'f', 'm', 'f'}, ...
'age' ,{25, 35, 30, 22, 38} ) ));
expected value 'anna' 'elaina'
KSSV
KSSV 2021년 11월 18일
Write the given code into a a function.

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

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2021년 11월 18일

편집:

2021년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by