How to extract column number of a variable in matlab table?
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Chang Li
il 31 Dic 2022
Commentato: Star Strider
il 3 Gen 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 Commenti
Risposta accettata
Star Strider
il 31 Dic 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 Commenti
Voss
il 3 Gen 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
il 3 Gen 2023
I had problems getting that to work when I tried it. That’s the reason I went with cellfun in the end.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Tables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!