Saving matlab code / good programming practice

5 次查看(过去 30 天)
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?

回答(2 个)

Geoff
Geoff 2012-3-23
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 个评论
Eli
Eli 2012-3-26
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 2012-3-26
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
Bjorn Gustavsson 2012-3-26
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.

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by