Look for string in table and return a number in the same row different column

1 view (last 30 days)
Hi i have a table named TurbulentFittings.xlsx
and i am trying to return values from speKL column by using fitcode entries kind of like VLOOKUP but for string lookup.
here's my code
KLDATA = readtable("TurbulentFittings.xlsx")
fitcode = app.FittingCodeEditField.Value %Fitcode entry
speKL = vlookup("fitcode",KLDATA,2,3,0) %how do i look for the speKL value in the same row as the fitcode? vlookup doesnt work

Accepted Answer

Steven Lord
Steven Lord on 21 Sep 2021
I'd probably use matches for text data (and the normal relational operators for numeric data.)
load patients
P = table(LastName, Age, Gender, Height, Weight);
head(P)
ans = 8×5 table
LastName Age Gender Height Weight ____________ ___ __________ ______ ______ {'Smith' } 38 {'Male' } 71 176 {'Johnson' } 43 {'Male' } 69 163 {'Williams'} 38 {'Female'} 64 131 {'Jones' } 40 {'Female'} 67 133 {'Brown' } 49 {'Female'} 64 119 {'Davis' } 46 {'Female'} 68 142 {'Miller' } 33 {'Female'} 64 142 {'Wilson' } 40 {'Male' } 68 180
Now let's find all patients that are male and are at least 40 years old.
areMale = matches(P.Gender, 'Male'); % Use matches for text
are40OrOlder = P.Age >= 40; % Use relational operators for numbers
P(areMale & are40OrOlder, :)
ans = 22×5 table
LastName Age Gender Height Weight ____________ ___ ________ ______ ______ {'Johnson' } 43 {'Male'} 69 163 {'Wilson' } 40 {'Male'} 68 180 {'Martin' } 48 {'Male'} 71 181 {'Robinson'} 50 {'Male'} 68 172 {'Scott' } 47 {'Male'} 70 187 {'Green' } 44 {'Male'} 71 193 {'Baker' } 44 {'Male'} 71 192 {'Perez' } 44 {'Male'} 69 183 {'Roberts' } 44 {'Male'} 70 169 {'Phillips'} 45 {'Male'} 67 172 {'Edwards' } 42 {'Male'} 70 158 {'Collins' } 42 {'Male'} 67 179 {'Stewart' } 49 {'Male'} 68 170 {'Reed' } 50 {'Male'} 72 186 {'Bell' } 45 {'Male'} 70 170 {'Barnes' } 42 {'Male'} 66 194
The row for Mr. Smith, row 1 in P, doesn't show up because he's not old enough. The row for Mr. Johnson, row 2 in P, does show up because he's a male over 40. None of the next 5 lines show up because those patients aren't male. If I'd wanted I could have just gotten the heights of those patients.
% Use curly braces to extract the contained data not a subtable
H = P{areMale & are40OrOlder, 'Height'}
H = 22×1
69 68 71 68 70 71 71 69 70 67

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!