Using string variable names for dot indexing
조회 수: 20 (최근 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!