Save currently running script in Matlab

筅森魡賤 提交于 2019-12-12 18:52:24

问题


Is there a way of saving the currently running script in Matlab? I have a script which automatically takes a backup of a set of scripts although if I have changed the current script then the saved version will be out of date.

Perhaps its possible to call some java?

Thanks


回答1:


Somewhere on Yair Altman's site (see link in my other answer) he also referred to a blog entry about editorservices, which was introduced with MATLAB R2009b.

editorservices.getActive().save

should do what you want.




回答2:


Okay, all I write here I learned from undocumentedmatlab.com by Yair Altman, in particular by looking into his EditorMacro.m... great stuff!

I'm assuming that Itamar Katz understood you correctly and that you are running unsaved code from the editor by using "Evaluate Cell" or "Evaluate Selection"; you want your code to realize that it is not saved and save the version currently displayed in the editor to some other location.

I have not found a way to save the file directly to the original location, but at least I have found a way to access the current text. You can then use fprintf to save it to wherever you want. I have tested this in Matlab 7.11 (R2010b); if you have a different version you'd need to dig through EditorMacro.m to find the correct code for Matlab 6.

if  com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.isDirty    
    thisdocument=com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getDocument;
    thisdocument_text=char(thisdocument.getText(0,thisdocument.getLength));
    fid = fopen('backupfile.m','w');
    fprintf(fid, '%s', thisdocument_text);
    fclose(fid);
else
    % saved file is unmodified in editor - no need to play tricks...
    ...
end

So the if-condition checks if the currently active editor window contains a file which is not saved ("dirty"); if it is, we need to retrieve the current version of the code (into variable thisdocument_text) and save this string to some file.

Does this help?



来源:https://stackoverflow.com/questions/5389528/save-currently-running-script-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!