Using string variable names for dot indexing

19 vues (au cours des 30 derniers jours)
Karel Starý
Karel Starý le 7 Mar 2024
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!

Réponse acceptée

Star Strider
Star Strider le 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)
Test = 5×10 table
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 ________ _________ __________ __________ _________ _________ ____________ ____________ ______________ ______________ 0.53165 -1.4146 0.15515 1.656 -1.7371 1.3253 1.6272 -0.44658 0.97248 0.87074 0.041848 2.0438 0.15903 -0.51318 -1.0633 1.0485 -1.7061 -0.52948 -1.2473 -0.39379 0.51137 -0.50428 0.22443 0.79484 0.76808 0.65757 -0.16261 -0.86027 -0.86237 -2.0298 -1.0916 -0.024071 0.85436 0.40742 0.4719 -0.53527 0.64941 0.10239 0.76209 -0.10233 1.0462 -1.4834 -0.36448 -0.24396 -0.16983 0.93012 -0.30279 0.40905 1.4175 0.62576
rr = 2;
vn = varnames(rr)
vn = 1×1 cell array
{'sinr_nr'}
GetColumn = Test.(varnames{rr})
GetColumn = 5×1
-1.4146 2.0438 -0.5043 -0.0241 -1.4834
That also works if the variable name is not a normal MATLAB variable name (for example containing a space or other special characters).
.

Plus de réponses (1)

Rik
Rik le 7 Mar 2024
You're missing parentheses:
varnames = {'rsrp_nr' 'sinr_nr'};
T=table(pi,2*pi,VariableName={'rsrp_nr' 'sinr_nr'})
T = 1×2 table
rsrp_nr sinr_nr _______ _______ 3.1416 6.2832
T.(varnames{2})
ans = 6.2832

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by