Saving matlab code / good programming practice
5 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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
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 );
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.
0 Comments
See Also
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!