How can i change variable name by code???

8 visualizaciones (últimos 30 días)
jaewoo kim
jaewoo kim el 24 de Mzo. de 2021
Editada: Stephen23 el 24 de Mzo. de 2021
Hello sir..
I faced a awesome problem
in my code
ext = '*.txt';
files = dir(strcat(pathDir,slash,ext));
for i=1:length(files)
B = rand(63);
C = files(i).name
????
end
in a loop, i want to rename variable B to C
How can i do that??
  1 comentario
Stephen23
Stephen23 el 24 de Mzo. de 2021
Editada: Stephen23 el 24 de Mzo. de 2021
"I faced a awesome problem"
Which is easily solved using much better** indexing, just as the documentation shows:
** better in the sense simpler, neater, much more efficient, less buggy, and easier to debug than what you are attempting.
"i want to rename variable B to C"
Consider what will happen if the file is named "1+2", or "quit" or anything that is not a valid variable name, e.g. anything ending with file-extension .txt (like your example).
Why do you want to force yourself into writing complex, obfuscated, buggy code?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 24 de Mzo. de 2021
Editada: Jan el 24 de Mzo. de 2021
rename variable B to C:
C = B;
I'm afraid you want top do something else: Creating a variable with a dynamically determined name. This is a bad programming practize. Don't do it, because you drill a whole inyour own knee. See TUTORIAL: Why and how to avoid Eval

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by