Any alternative to using the eval command

Hi,
Is there a way to execute the following command without using the eval command?
eval(['save ADCP', num2str(k), ' U V W']);
I want to save the variables U V W to a mat file called ADCPx where x is a number that changes in each iteration of a loop.
The above command works fine but I was wondering if there was an alternative way?
Thanks

5 comentarios

Jan
Jan el 23 de Feb. de 2018
+1: A smart idea to search for an alternative to eval. See also Tutorial: Do not eval.
Stephen23
Stephen23 el 23 de Feb. de 2018
Editada: Stephen23 el 23 de Feb. de 2018
"I want to save the variables U V W to a mat file called ADCPx where x is a number that changes in each iteration of a loop."
How do you get those numbers into the variable names in the first place?
You could easily avoid this whole issue by not adding numbers onto variable names. A number implies that you have a de-facto index, in which case why not just turn it into a real index, which would be simpler, more efficient, less buggy, and easier to debug. And you would avoid magically accessing variable names, wherever that might occur in your code.
Jan
Jan el 23 de Feb. de 2018
@Stephen: ADCP1 is the name of the MAT-file, not a numbered variable. For files in the file system, indexing is not implemented by the modern operating systems, unfortunately. I loved Bill Gates' idea of omitting the complete file system stuff and replace it by a data base. Organizing files in folders is a stone age method. But, sigh, Mr. Gates decided to develop ribbons and tiles at first, while the file system still limits the length of the names to 260 characters.
Stephen23
Stephen23 el 23 de Feb. de 2018
@Jan Simon: I must have gone blind for a moment. You are quite right.
Guillaume
Guillaume el 23 de Feb. de 2018
"Organizing files in folders is a stone age method"
Agreed! And using said folder organisation as an integral part of your programming language is even worse. Looking at you there, matlab with your @folder, +folder, private folder, etc.!

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 23 de Feb. de 2018
save(sprintf('ADCP%d', k), 'U', 'V', 'W');

2 comentarios

Jan
Jan el 23 de Feb. de 2018
+1. A perfect answer.
Chris O'Donnell
Chris O'Donnell el 23 de Feb. de 2018
Thanks for this!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Feb. de 2018

Comentada:

el 23 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by