How to extract column number of a variable in matlab table?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2022-12-31
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 个评论
Voss
2023-1-3
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
2023-1-3
I had problems getting that to work when I tried it. That’s the reason I went with cellfun in the end.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!