How to extract column number of a variable in matlab table?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chang Li
am 31 Dez. 2022
Kommentiert: Star Strider
am 3 Jan. 2023
How to extract column number of a variable in matlab table? For example, the column number of the variable A2 in a table: table.A2 is 2. How do I use “table.A2” as input to extract the column#: 2 and save 2 in another variable? Thank you very much.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 31 Dez. 2022
One approach —
T1 = array2table(randn(7,5), 'VariableNames',{'A1','A2','A3','A4','A5'})
VN = T1.Properties.VariableNames;
Lvc = cellfun(@(x)strcmp(x,'A2'), VN, 'Unif',0);
ColNr = find(cell2mat(Lvc))
.
2 Kommentare
Voss
am 3 Jan. 2023
Note that strcmp works with a cell array of character vectors, so cellfun and cell2mat are unnecessary in this case:
T1 = array2table(randn(7,5), 'VariableNames',{'A1','A2','A3','A4','A5'})
VN = T1.Properties.VariableNames;
ColNr = find(strcmp(VN,'A2'))
Star Strider
am 3 Jan. 2023
I had problems getting that to work when I tried it. That’s the reason I went with cellfun in the end.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Tables 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!