extracting a string and using that as a variable name
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 13 Avr 2018
Modifié(e) : Stephen23
le 13 Avr 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.
Réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Compiler dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!