Using string variable names for dot indexing

조회 수: 20 (최근 30일)
Karel Starý
Karel Starý 2024년 3월 7일
답변: Star Strider 2024년 3월 7일
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!

채택된 답변

Star Strider
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)
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).
.

추가 답변 (1개)

Rik
Rik 2024년 3월 7일
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by