Using string variable names for dot indexing
20 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 7 Mar 2024
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 Comments
More Answers (1)
Rik
on 7 Mar 2024
You're missing parentheses:
varnames = {'rsrp_nr' 'sinr_nr'};
T=table(pi,2*pi,VariableName={'rsrp_nr' 'sinr_nr'})
T.(varnames{2})
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!