フィルターのクリア

How to select a specific element in a table using variables corresponding to table headers

14 ビュー (過去 30 日間)
Hello all,
Imagine I have the following table T:
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

採用された回答

Stephen23
Stephen23 2021 年 5 月 31 日
編集済み: Stephen23 2021 年 5 月 31 日
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)
T1 = 4×2 table
Order Lambda _____ ______ Catherine 15 12 Bob 13 14 Fred 13 21 Anna 4 46
out = T1{name,'Order'}
out = 15
Without row names requires the use of indexing (e.g. via STRCMP or similar):
T2 = table(Ref,Order,Lambda)
T2 = 4×3 table
Ref Order Lambda _____________ _____ ______ {'Catherine'} 15 12 {'Bob' } 13 14 {'Fred' } 13 21 {'Anna' } 4 46
idx = strcmpi(T2.Ref,name);
out = T2{idx,'Order'}
out = 15

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by