rename a variable

5 vues (au cours des 30 derniers jours)
Baba
Baba le 5 Mar 2012
in my workspace i have an array that is named 'data' and I have a name field with some filename = 'filename'
I would like to rename the array called data to the name that is stored in the filename name variable.
simply doing filename = data; renames data to filename not the actual name that filename holds.

Réponse acceptée

Matt Tearle
Matt Tearle le 5 Mar 2012
Let me see if I interpret the question correctly: you have
filename = 'bob';
data = [1,2,3];
And you want to rename data to bob (the contents of filename)?
Firstly, be careful doing things like this. It makes it very hard to track names. But, if you must...
str = [filename,'= data;'];
eval(str);
  1 commentaire
Walter Roberson
Walter Roberson le 5 Mar 2012
"if you must" then you probably have not written your program in a good way.
Baba, use dynamic structure field names of a single structure variable that will contain all of the data.

Connectez-vous pour commenter.

Plus de réponses (1)

Sean de Wolski
Sean de Wolski le 5 Mar 2012
Similar to FAQ

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by