isolating values in a table based on user input
1 view (last 30 days)
Show older comments
Hi All,
Hope all is well. I am trying to isolate data in a table based on user input. I have the below-
How do I loop through the table to get the values based on the user input. Many thanks, Best, Andrew
sex=input("please input the gender (M/F): ", 's');
sysbp= input("enter styloic blood pressure: ");
diabp= input("enter dystolic blood pressure: ");
systoliclow=[-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;
120;120;120;120;120;120;120;120;120;120;
130;130;130;130;130;130;130;130;130;130;
140;140;140;140;140;140;140;140;140;140;
160;160;160;160;160;160;160;160;160;160]
systolichigh =[119;119;119;119;119;119;119;119;119;119;
129;129;129;129;129;129;129;129;129;129;
139;139;139;139;139;139;139;139;139;139;
159;159;159;159;159;159;159;159;159;159;
inf;inf;inf;inf;inf;inf;inf;inf;inf;inf]
diastoliclow=[-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90; 100; 100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ]
diastolichigh=[79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf]
gender={'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' ;
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' }
values= [-3 ;0; 0 ;0; 0; 1; 2 ;2; 3 ;3;
0;0;0;0;0;1;2;2;3;3;
0;1;0;1;0;1;2;2;3;3;
2;2;2;2;2;2;2;2;3;3;
3;3;3;3;3;3;3;3;3;3]
bpt= table(systoliclow, systolichigh, diastoliclow,diastolichigh, gender, values)
if sysbp>=bpt.systoliclow && sysbp<=bpt.systolichigh && diabp<=bpt.diastoliclow && diap>=bpt.diastolichigh && ismember(sex{'F','M'});
test = bpt.values
Many thanks in advance.
Best,
Andrew
2 Comments
Adam Danz
on 7 Mar 2019
Let's say my inputs were "M", "117" and "71". What would you like to do with those data?
Accepted Answer
Adam Danz
on 7 Mar 2019
Edited: Adam Danz
on 8 Mar 2019
This solution creates a set of logical vectors that identify rows of dpt table that are satisfied by the user input. It then combines those logical vectors to identify the row(s) that is satisfied by all inputs. Next I added a sanity check that makes sure that one and only one row was selected. Then it returns the 'value' of that row.
isGenderMatch = strcmpi(bpt.gender, sex); % index of rows that match sex
isLessThanSys = sysbp < bpt.systolichigh; % index of rows where systl bp is less than 'high'
isGreaterThansSys = sysbp >= bpt.systoliclow; % index of rows where systl bp is greater or == than 'low'
isLessThanDia = diabp < bpt.diastolichigh; % index of rows where diast bp is less than 'high'
isGreaterThansDia = diabp >= bpt.diastoliclow; % index of rows where diast bp is greater or == than 'low'
rowIdx = isGenderMatch & isLessThanSys & isGreaterThansSys & isLessThanDia & isGreaterThansDia; %row index of all match(es)
% Sanity check
if sum(rowIdx) == 0
error('No matches found.')
end
if sum(rowIdx) > 1
error('More than 1 match found! Rows [%s].', num2str(find(rowIdx)))
end
% Return value of matching row
test = bpt.values(rowIdx);
1 Comment
More Answers (1)
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction 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!