How to access and change variables in workspace without knowing their names?

1 visualización (últimos 30 días)
Hi everybody
I would like to access and make changes to variables in workspace without knowing their names. I would like to call them one bye one and apply some changes into each variable. I might be able to use "whos" function to know names, but I do not know how to make changes. For example:
List = whos;
As known, List(1).name represents name of the first variable in the list. How can we change the elements of that variable. Do "evalin" or "assignin" work? How?
Thanks
  2 comentarios
per isakson
per isakson el 13 de Jun. de 2016
Editada: per isakson el 13 de Jun. de 2016
If you are familiar with all the reasons why not using eval you could do something like
eval( [list(1).name,' = 17;'] )
Stephen23
Stephen23 el 13 de Jun. de 2016
Editada: Stephen23 el 13 de Jun. de 2016
@per isakson: why assume that every beginner knows why this is a bad idea and should be avoided? Oftentimes they think that using dynamic variable names is a great idea!

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 13 de Jun. de 2016
Editada: Stephen23 el 28 de Jun. de 2021
Do NOT do this
This would actually be horribly slow, pointlessly complex, obfuscated, and very buggy way to write your code. You would make your code hard to debug, and a pain to work with. Read this to know why:
Using indexing would be simple, neat, and very efficient. You should use one array and indexing. Indexing is the solution you should be using, because it is the most efficient way to write MATLAB code, is simple, neat, and very easy to debug. Indexing is explained quite well in the MATLAB introductory tutorials, which are highly recommended for all beginners:

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by