extracting a string and using that as a variable name

3 visualizzazioni (ultimi 30 giorni)
minomi
minomi il 13 Apr 2018
Commentato: minomi il 13 Apr 2018
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 Commenti
Stephen23
Stephen23 il 13 Apr 2018
Modificato: Stephen23 il 13 Apr 2018
"...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 il 13 Apr 2018
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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su MATLAB Compiler in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by