How to extract column number of a variable in matlab table?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Star Strider
le 31 Déc 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 commentaires
Voss
le 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
le 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!