Saving matlab code / good programming practice

5 views (last 30 days)
Eli
Eli on 22 Mar 2012
Hi All,
I intend to run a certain script with some variations of the parameters and perhaps some minor changes to the code itself. I want an easy method to keep track on all the output files (i.e, which file resulted from each run). The best method I could think of is creating a variable that'll contain the script code and saving the modified workspace. How can I do that (remember the script code changes each run)? Also, what would you do in this situation (various parameters, various output files (workspaces))? What is a good programming practice?

Answers (2)

Geoff
Geoff on 23 Mar 2012
You might want your function to store the run date/time when invoked, and store both itself and its output. That way, any changes you make to the file will have a snapshot that you can link back to the output.
So, let's say your function is called Volatile =)
function Volatile( args )
datestamp = datestr( now, 'yyyymmdd_HHMMSS');
outdir = 'MyOutput/';
sourcename = [outdir datestamp '.m'];
inputsname = [outdir datestamp '.in'];
outputsname = [outdir datestamp '.out'];
% Store current source file.
s = dbstack('-completenames');
copyfile( s(1).file, sourcename );
% Store the inputs
%[TODO]
% Do some processing
%[TODO]
% Store the outputs
%[TODO]
end
For saving inputs and outputs, you may want to do this within the function, or you may want to do it outside. Using the save() mechanism might be easiest for this, if you are only interested in retrieving the data with MatLab.
  2 Comments
Eli
Eli on 26 Mar 2012
Thanks Geoff,
It seems that this suggestion works, but only in functions. I was wondering if it is possible to do in scripts as well. Or even better, a modification, as follows:
Assume that I have a script called ttt.m . It contain:
a=b+1;
c=a*2;
...
I want to add a command/function that will create a variable called "source" which will contain
source=
"a=b+1
c=a*2;
..."
and to save it with the workspace. it is possible to automatically generate such parameter "source"? If not, how can I apply this method to scripts.
Geoff
Geoff on 26 Mar 2012
If dbstack() doesn't work in scripts, then just hard-code the filename instead. If you want to get 'source', which is a result of a,b,c then this is what functions are for:
source = myfunc( a, b, c );

Sign in to comment.


Bjorn Gustavsson
Bjorn Gustavsson on 26 Mar 2012
Write functions that does the job. Then one script that loads the data, calls the functions and saves the results. You can use the publish utility on the script and store that as well - this might be better that reading the script (and functions) into string variables and save those. If you really want to store snapshots of things you could also use CVS, SVN or other version control systems to store both source code and result files.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!