How to extract column number of a variable in matlab table?
11 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 31 Dec 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 Comments
Voss
on 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
on 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.
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!