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?
0 个评论
回答(2 个)
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!