Azzera filtri
Azzera filtri

isolating values in a table based on user input

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Adam Danz
Adam Danz il 7 Mar 2019
Let's say my inputs were "M", "117" and "71". What would you like to do with those data?
Andrew Czeizler
Andrew Czeizler il 7 Mar 2019
117 is between -inf and 119
71 is between -inf and 79
sex is male
so value is 0
i want to be able to find value based on input.
many thanks,
best,
Andrew

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 7 Mar 2019
Modificato: Adam Danz il 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 Commento
Adam Danz
Adam Danz il 8 Mar 2019
1 correction made. I had to add num2str() in the second sanity check.

Accedi per commentare.

Più risposte (1)

Andrew Czeizler
Andrew Czeizler il 7 Mar 2019
Thank you so much for your help!!
Really clear, what a champion!
Best,
Andrew

Community Treasure Hunt

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

Start Hunting!

Translated by