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
what changes is required in the code so that i can only display femate above age of 30 and less that 40
function [patient] = filterpatients_cell(data)
colage = data(strcmp(data(1,:),'age'),1);
if colage == 30 && colage <= 40
col = strcmp(data(1,:),'gender');
out = data(strcmp(data(:,col),'f'),1);
end
patient = out;

 채택된 답변

KSSV
KSSV 2021년 11월 18일

1 개 추천

A table would be good to deal with.
name = {'mary','john','anna','paul','elaina'}' ;
gender = {'f', 'm', 'f', 'm', 'f'}' ;
age = [25, 35, 30, 22, 38]' ;
T = table(name,gender,age)
T = 5×3 table
name gender age __________ ______ ___ {'mary' } {'f'} 25 {'john' } {'m'} 35 {'anna' } {'f'} 30 {'paul' } {'m'} 22 {'elaina'} {'f'} 38
% Get females
idx = strcmp(T.gender,'f') ;
% GEt age
T1 = T(idx,:) ;
idx = T1.age >= 30 & T1.age <= 40 ;
T2 = T1(idx,:)
T2 = 2×3 table
name gender age __________ ______ ___ {'anna' } {'f'} 30 {'elaina'} {'f'} 38

댓글 수: 3

Manav Divekar
Manav Divekar 2021년 11월 18일
편집: Manav Divekar 2021년 11월 18일
is it possible with cell array? instead of table as i would like to only display the names at the end. and don't want to manually enter the input as given value is a small part of the database.
KSSV
KSSV 2021년 11월 18일
It is possible with cell array. Other question is not clear.
Manav Divekar
Manav Divekar 2021년 11월 18일
Can you explain how it is possible using cell array?

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

추가 답변 (0개)

카테고리

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

제품

릴리스

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