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 Comments

+1: A smart idea to search for an alternative to eval. See also Tutorial: Do not eval.
Stephen23
Stephen23 on 23 Feb 2018
Edited: Stephen23 on 23 Feb 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.
@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.
@Jan Simon: I must have gone blind for a moment. You are quite right.
"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.!

Sign in to comment.

 Accepted Answer

save(sprintf('ADCP%d', k), 'U', 'V', 'W');

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!