extracting a string and using that as a variable name
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a code which results in producing a cell array called var_name consisting of two rows:
var_name =
3×1 cell array
{'x1' }
{'x2' }
I want to use the each cell of the array to be used as a variable name and use it in when I gather tall array variables. So far this is what I have done:
eval([var_name{1},var_name{2} '= gather(tt.var_name{1},tt.var_name{1})']);
but it does not work as the right hand side is not quite right and I get an error that var_name is unrecognized. Any help would be appreciated.
댓글 수: 2
Stephen23
2018년 4월 13일
편집: Stephen23
2018년 4월 13일
"...but it does not work as the right hand side is not quite right and I get an error that var_name is unrecognized."
Well, you have chosen a way of writing code that is slow, complex, buggy, and makes debugging hard. Now you have a bug that is hard to debug, which is not a surprise.
"Any help would be appreciated."
Avoid accessing variable names dynamically. Accessing variable names dynamically is how beginners force themselves into writing slow, complex, obfuscated code that they cannot debug. Read this to know more:
If you explained what you are actually trying to achieve then most likely we can show you simpler code for that task. Please explain what you intend on doing with those variables, once they have been created in the workspace.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!