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 Comment

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

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 25 Mar 2015
Edited: Stephen23 on 25 Mar 2015
In a word: dbstack. You could then use the '-completenames' option to get the fullnames as well.

Categories

Asked:

on 25 Mar 2015

Edited:

on 25 Mar 2015

Community Treasure Hunt

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

Start Hunting!