在 Matlab 中保存当前运行的脚本

发布于 2024-10-24 07:37:18 字数 102 浏览 10 评论 0原文

有没有办法保存Matlab中当前运行的脚本?我有一个脚本,它会自动备份一组脚本,但如果我更改了当前脚本,则保存的版本将过期。

也许可以调用一些java?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-10-31 07:37:18

在 Yair Altman 网站的某个地方(请参阅我的其他答案中的链接),他还提到了 有关编辑器服务的博客文章,它是随 MATLAB R2009b 引入的。

editorservices.getActive().save

应该做你想做的事。

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.

铃予 2024-10-31 07:37:18

好的,我在这里写的所有内容都是从 undocumentedmatlab.com 作者:Yair Altman,特别是通过研究他的EditorMacro.m...很棒的东西!

我假设 Itamar Katz 正确理解了您的意思,并且您正在使用“评估单元格”或“评估选择”从编辑器运行未保存的代码;您希望您的代码意识到它未保存,并将编辑器中当前显示的版本保存到其他位置。

我还没有找到将文件直接保存到原始位置的方法,但至少我找到了访问当前文本的方法。然后您可以使用 fprintf 将其保存到您想要的任何位置。我已经在 Matlab 7.11 (R2010b) 中对此进行了测试;如果您有不同的版本,则需要深入研究 EditorMacro.m 来找到 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

因此,if 条件检查当前活动的编辑器窗口是否包含未保存的文件(“脏”);如果是,我们需要检索代码的当前版本(到变量 thisdocument_text 中)并将该字符串保存到某个文件中。

这有帮助吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文