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

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

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2018년 2월 23일

1 개 추천

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

댓글 수: 2

Jan
Jan 2018년 2월 23일
+1. A perfect answer.
Thanks for this!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

질문:

2018년 2월 23일

댓글:

2018년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by