Finding a variable in a large table

5 ビュー (過去 30 日間)
George
George 2022 年 12 月 8 日
コメント済み: Voss 2022 年 12 月 9 日
I'm quite a novice to MATLAB so apologies for any language I may use which may be confusing
I have a 2570x16 sized table, which relates to data about every UK train station, i.e. usage statistics, I have written a menu function which allows the selected station to become a string, what do I need to do so that the Station can be found in the name column, and then this can be used to recall the other 15 parts of data about the station?
Many thanks

採用された回答

Voss
Voss 2022 年 12 月 8 日
% a table with station names and other info:
t = table(["Picadilly Circus"; "St. John's Wood"; "Cockfosters"],rand(3,1),rand(3,1), ...
'VariableNames',{'name' 'other info 1' 'other info 2'})
t = 3×3 table
name other info 1 other info 2 __________________ ____________ ____________ "Picadilly Circus" 0.14357 0.81944 "St. John's Wood" 0.2586 0.93962 "Cockfosters" 0.47828 0.34349
% the station you want to know about:
station = "Cockfosters";
% get all the info about that station:
result = t(strcmp(t.name,station),:) % result is a table with one row
result = 1×3 table
name other info 1 other info 2 _____________ ____________ ____________ "Cockfosters" 0.47828 0.34349
% and/or get just the "other info" for that station:
% (this assumes 'name' is the first column of the table)
result = t{strcmp(t.name,station),2:end} % result is a numeric array in this case
result = 1×2
0.4783 0.3435
  2 件のコメント
George
George 2022 年 12 月 9 日
Thnak you!
Voss
Voss 2022 年 12 月 9 日
You're welcome!

サインインしてコメントする。

その他の回答 (2 件)

Jon
Jon 2022 年 12 月 8 日
編集済み: Jon 2022 年 12 月 8 日
Here's a simple example, which I think you could extend to your situation
% make an example table
name = {'fish','cat','dog','cat','mouse','mouse','fish','cat'}'
weight = [0.8, 1.3, 2.2, 1.1, 0.2, 0.3, 0.9, 1.4]'
bodyLength = [102,503,608, 429,15,18, 154,496]'
T = table(name,weight,bodyLength)
% get the weights and lengths of all of the cats
idl = strcmp('cat',T.name);
Tcat = T(idl,:) % a table with just cat data
% or if you just want the weight of the fish
idl = strcmp('fish',T.name);
wFish = T.weight(idl)

Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 8 日
編集済み: Bora Eryilmaz 2022 年 12 月 8 日
% Create a table
T = table;
T.Name = ["London"; "Paris"];
T.Data1 = {15; 20};
T.Data2 = {-1; 2};
T
T = 2×3 table
Name Data1 Data2 ________ ______ ______ "London" {[15]} {[-1]} "Paris" {[20]} {[ 2]}
% Find index of Name
I = strcmp(T.Name, 'Paris');
% Find data for that Name
T{I,:}
ans = 1×3 string array
"Paris" "20" "2"
% or, extract data as its own 1-row table
T(I,:)
ans = 1×3 table
Name Data1 Data2 _______ ______ _____ "Paris" {[20]} {[2]}

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by