Getting the value of variable from its name

215 visualizaciones (últimos 30 días)
Pushkar Dhekne
Pushkar Dhekne el 4 de Jun. de 2021
Comentada: Stephen23 el 2 de Nov. de 2023
I have variable name saved as a string in an array. I want to read the value of the variable into another array. Is there a function in matla which takes variable name as input and returns the value of that variable.

Respuestas (1)

Benjamin Kraus
Benjamin Kraus el 4 de Jun. de 2021
Editada: Benjamin Kraus el 4 de Jun. de 2021
eval will do this, but see the link posted by Stephen explaining all the reasons you should avoid using eval: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
For example:
a = 1:10;
varName = 'a';
b = eval(varName);
  1 comentario
Stephen23
Stephen23 el 2 de Nov. de 2023
"see the link posted by Stephen explaining all the reasons you should avoid using eval:"
Correction: "see the link by Stephen which explains the main reasons why you should avoid forcing meta-data (e.g. pseudo-indices, test parameter values, etc.) into variable names or fieldnames."

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by