How to select a specific element in a table using variables corresponding to table headers
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rafael Cordero
am 31 Mai 2021
Bearbeitet: Stephen23
am 31 Mai 2021
Hello all,
Imagine I have the following table T:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/637220/image.png)
I would like to programatically extract a specific Order corresponding to a specific Reference that is given in a separate variable.
Example, for:
var_name = 'Charlie'
What is the syntax to extract Charlie's Order (i.e. 15) using var_name?
Thank you
0 Kommentare
Akzeptierte Antwort
Stephen23
am 31 Mai 2021
Bearbeitet: Stephen23
am 31 Mai 2021
How to select data from a table is explained here:
Ref = {'Catherine';'Bob';'Fred';'Anna'};
Order = [15;13;13;4];
Lambda = [12;14;21;46];
% name you want to match:
name = 'Catherine';
By far the easiest way is to use row names:
T1 = table(Order,Lambda,'RowNames',Ref)
out = T1{name,'Order'}
Without row names requires the use of indexing (e.g. via STRCMP or similar):
T2 = table(Ref,Order,Lambda)
idx = strcmpi(T2.Ref,name);
out = T2{idx,'Order'}
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!