Using string variable names for dot indexing
18 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a list of the varaible names:
varnames = {'rsrp_nr' 'sinr_nr' 'bler_dl_nr' 'bler_ul_nr' 'mcs_dl_nr' 'mcs_ul_nr' 'layers_dl_nr' 'layers_ul_nr' 'tp_pdsch_dl_nr' 'tp_pusch_ul_nr'};
And i would like to use this list to call a rows by the name f.a.:
rr=2
my_files{1,1}.varnames(rr)
I woudl like the code to process this as:
my_files{1,1}.sinr_nr
For now I am getting an error:
Error using tabular/dotParenReference
Unrecognized table variable name 'varnames'.
Thanks!
0 个评论
采纳的回答
Star Strider
2024-3-7
That approach can work, however it is necessary to put the variable name from the cell array in parentheses —
varnames = {'rsrp_nr' 'sinr_nr' 'bler_dl_nr' 'bler_ul_nr' 'mcs_dl_nr' 'mcs_ul_nr' 'layers_dl_nr' 'layers_ul_nr' 'tp_pdsch_dl_nr' 'tp_pusch_ul_nr'};
Test = array2table(randn(5,numel(varnames)), 'VariableNames',varnames)
rr = 2;
vn = varnames(rr)
GetColumn = Test.(varnames{rr})
That also works if the variable name is not a normal MATLAB variable name (for example containing a space or other special characters).
.
0 个评论
更多回答(1 个)
Rik
2024-3-7
You're missing parentheses:
varnames = {'rsrp_nr' 'sinr_nr'};
T=table(pi,2*pi,VariableName={'rsrp_nr' 'sinr_nr'})
T.(varnames{2})
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!