Andrew Czeizler
Andrew Czeizler

Reputation: 35

Matlab table - Searching and isolating values in a table

I am trying to isolate data in a table based on user input. I have the below-

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

How do I loop through the table to get the values based on the user input.

Upvotes: 1

Views: 72

Answers (2)

Paolo
Paolo

Reputation: 26074

You can use logical indexing, no loops required:

isIsolated = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(bpt.gender,sex);
isolatedTable = bpt(isIsolated,:);

For inputs M, 120 and 85:

>> isolatedTable 

isolatedTable =

  1×6 table

    systoliclow    systolichigh    diastoliclow    diastolichigh    gender    values
    ___________    ____________    ____________    _____________    ______    ______

        120            129              85              89           'M'        1   

If you are only interested in the values variable:

>> isolatedValues = bpt(isIsolated,end)

isolatedValues =

  2×1 table

    values
    ______

      1   

Upvotes: 2

Anton
Anton

Reputation: 4684

You can query the rows which fit the user input and iterate through their ids:

rows = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(sex, bpt.gender);

ind = find(rows == 1);

for i=ind
    bpt(i, :)
end

The result:

ans = 

    systoliclow    systolichigh    diastoliclow    diastolichigh    gender    values
    ___________    ____________    ____________    _____________    ______    ______

    -Inf           119             90              99               'F'       2     
    -Inf           119             90              99               'M'       2     

Upvotes: 0

Related Questions