Save a file copy of a script from within that script...

조회 수: 13 (최근 30일)
Justin
Justin 2011년 1월 27일
I'm interested in saving a copy of a script every time I run that script. I suppose that it's a kind of poor-man's version control. I'm varying the script a lot with time, and I'd like to ensure that in the future, when I look over the output from it, I can identify exactly which version I was using.
I have a very ugly way to do it right now, that uses the eval command and the ! syntax to call the DOS? copy command:
CopyName = ['"',ResultsDir,UniqueID,'.txt"'];
eval(['!copy MyScript.m ',CopyName])
I suppose that the ideal version would know the filename of the calling script, and not rely on the ! or eval syntax.
Any ideas?
Thanks, Justin

채택된 답변

Paulo Silva
Paulo Silva 2011년 1월 27일
This is how you can do a backup of your m file everytime you run it but matlab already does that for you, if you want to keep several backup files you must come up with a way to name them, maybe with a string with the date and hour or having a number that get incremented everytime your run the file.
FileNameAndLocation=[mfilename('fullpath')];
newbackup=sprintf('%sbackup.m',FileNameAndLocation);
currentfile=strcat(FileNameAndLocation, '.m');
copyfile(currentfile,newbackup);
With version number
Version='1';
FileNameAndLocation=[mfilename('fullpath')];
newbackup=sprintf('%sbackup%s.m',FileNameAndLocation,Version);
currentfile=strcat(FileNameAndLocation, '.m');
copyfile(currentfile,newbackup);
You can also check if the backup of a particular version already exists by doind
Version='1';
FileNameAndLocation=[mfilename('fullpath')];
newbackup=sprintf('%sbackup%s.m',FileNameAndLocation,Version);
A = exist(newbackup,'file')
if (A~=0)
warning('Backup already exists for the current version')
end
  댓글 수: 3
Justin
Justin 2011년 3월 9일
This is all very cool. Thanks.
Jan
Jan 2011년 3월 9일
@Oleg: COPYFILE is much faster than calling COPY in a DOS box: Opening the DOS box takes a lot of time already.
If the file is not saved before calling, the old version is used for processing also. Therefore the task needed by the OP is accomplished by Paulo's method: Save the script file, which processed the data.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by