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 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!