How to write a sophisticated procedure for saving?

I would like to write a function, which can save the workspace with the following properties: 1) the name of the output file contains the name of the script 2) the name of the output file also contains the date and time 3) the location of the output file will be the same folder (the folder of the script)
I can write this as a subroutine which can be inserted at the end of any script, but I would like to make a function, which can be called at the end of the script like: mysave(); The problem is in this case that the location and the name of the script differs from the location and name of mysave() function, and I can extract only the later info. How to know the location or name of the script in which the mysave function is called?

댓글 수: 1

hmm... the location and name of the script are actually not the problem in my opinion. For this, you could simply do:
function mysave(script_fullpath)
[ path, name, ~] = fileparts(script_fullpath);
% save xy
end
In the function itself, you could call it with mysave(mfilename('fullpath')). The challenge I see is that if you call an external function, you move into a new workspace, hence not saving what you want.
I would stick with something like
date = clock;
date = regexprep(num2str(date(1:5)),' +','_');
save([ fileparts(mfilename('fullpath')) '/' date '_' mfilename '_ws' ])
Of course, you could save the name_generation in an external file, but idk if that's really worth it. Maybe someone else knows how to pass the whole workspace

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

답변 (1개)

Stephen23
Stephen23 2015년 3월 25일
편집: Stephen23 2015년 3월 25일

0 개 추천

In a word: dbstack. You could then use the '-completenames' option to get the fullnames as well.

카테고리

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

질문:

2015년 3월 25일

편집:

2015년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by