2016-10-01 127 views
2

当编辑器中的多个文件变脏时,经常出现运行我的main.m的错误。以编程方式将所有脏文件保存在MatLab中

这将是很好,如果我可以在我的main.m开始时有一个命令,只是自动保存每个脏文件。

Save currently running script in Matlab给出了用于保存当前活动文件线索的答案,但有没有办法为ALL文件做到这一点?

+2

我建议你明确的“脏”在这里的意思为益(已修改但尚未保存的文件)非母语者 –

回答

3

您可以使用com.mathworks.mlservices.MLEditorservice对象访问编辑器并保存所有脏文件。

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 

而且这可能与多个函数名被称为(字符串)

saveAll('myfunc', 'myotherfunc') 
+0

谢谢! (顺便提一句,由于基于0的索引,即似乎editors.get(k-1);'是必要的)。不幸的是,这个解决方案并不完美。如果我肮脏我的'main.m'文件(包含该脚本),然后运行它我得到***“java.lang.Exception:java.lang.RuntimeException:无法保存到main.m,而它正在被调试退出调试模式,然后再试一次。“*** –

+0

@Pi感谢您指出索引问题。它说你处于调试模式,是这种情况吗?当您不在调试模式时,您是否尝试过运行它? – Suever

相关问题