以编程方式在MatLab中保存所有脏文件

以编程方式在MatLab中保存所有脏文件,matlab,macros,editor,Matlab,Macros,Editor,当编辑器中的多个文件变脏时,我经常犯运行我的main.m的错误 如果在main.m的开头有一个命令可以自动保存所有脏文件,那就太好了 给出一个带有保存当前活动文件线索的答案,但是有没有办法保存所有文件?您可以使用该对象访问编辑器并保存所有脏文件 service = com.mathworks.mlservices.MLEditorServices; % Get a vector of all open editors editors = service.getEditorApplication

当编辑器中的多个文件变脏时,我经常犯运行我的
main.m
的错误

如果在
main.m
的开头有一个命令可以自动保存所有脏文件,那就太好了

给出一个带有保存当前活动文件线索的答案,但是有没有办法保存所有文件?

您可以使用该对象访问编辑器并保存所有脏文件

service = com.mathworks.mlservices.MLEditorServices;

% Get a vector of all open editors
editors = service.getEditorApplication.getOpenEditors();

% For each editor, if it is dirty, save it
for k = 0:(editors.size - 1)
    editor = editors.get(k);

    if editor.isDirty()
        editor.save();
    end
end
不必盲目地保存所有文件,您可以稍微修改它,以便传递函数列表(您的依赖项)并只保存它们

function saveAll(varargin)
    % Convert all filenames to their full file paths
    filenames = cellfun(@which, varargin, 'uniformoutput', false);

    service = com.mathworks.mlservices.MLEditorServices;

    % Get a vector of all open editors
    editors = service.getEditorApplication.getOpenEditors();

    % For each editor, if it is dirty, save it
    for k = 0:(editors.size - 1)
        editor = editors.get(k);

        % Check if the file in this editor is in our list of filenames
        % and that it's dirty prior to saving it
        if ismember(char(editor.getLongName()), filenames) && editor.isDirty()
            editor.save();
        end
    end
end
这可以用多个函数名(作为字符串)调用


我建议您在此处澄清“脏”的含义(已修改但未保存的文件),以方便非母语使用者。谢谢!(顺便说一句,它显示为
editors.get(k-1);
是必需的,因为它是基于0的,即健全的索引)。不幸的是,这个解决方案不太管用。如果我弄脏了我的
main.m
文件(包含该脚本),然后运行它,我会得到“java.lang.Exception:java.lang.RuntimeException:无法在调试main.m时保存到它。请退出调试模式并重试。”@Pi感谢您指出索引问题。它说您处于调试模式,是这样吗?您是否尝试过在不处于调试模式时运行它?
saveAll('myfunc', 'myotherfunc')