How to change a variable name without changing the value itself?
Mostrar comentarios más antiguos
I've got some variables like these: OutArray, OutArray2 and so on...
I need to refresh the base name OutArray adding the "1", "2", "3"... taking advantage of the loop iterator, how may I do it?
I have MATLAB R2023A
Respuesta aceptada
Más respuestas (4)
Star Strider
el 10 de Jun. de 2023
2 votos
chicken vector
el 10 de Jun. de 2023
1 voto
This is in general not recommend because is prone tu bugs and affects code readibility.
The easy way
variables = ["OutArray" "OutArray2" "OutArray3"];
for kk = 1:numel(variables)
eval(variables(kk) + string(kk) + " = eval(variables(kk))");
clear(variables(kk))
end
A more refined way
variables = string(who); % retrieve the name of the variables from workspace
% without having to write them by hand
for kk = 1:numel(variables)
eval(variables(kk) + string(kk) + " = eval(variables(kk))");
clear(variables(kk))
end
1 comentario
John D'Errico
el 10 de Jun. de 2023
Editada: John D'Errico
el 10 de Jun. de 2023
Neither of those ways is at all refined. And neither of them are a remotely good idea. They will lead to crappy, buggy, and SLOW code, when a simple array was all that was ever needed. Is there even a remotely good reason to recommend this? NO.
Antonio Victoria
el 13 de Jun. de 2023
0 votos
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
