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
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.
minomi
minomi 2018-4-13
I have a set of data files, starting with a description section including node names & sampling rates. I then have a spreadsheet with the node names, and their corresponding variable names (and some other related parameters). I have a script that reads the description section of the data file, finds the node number, then looks at the excel file and finds the corresponding variable names i.e. var_name. I then create a data store for the data file and change the headings to the variable names of interest. I then create a tall array and need to use gather to extract the variables.
I hope this is clear.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by