2016-10-01 12 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

ありがとう! (btwは、editors.get(k-1);は0ベースのために必要である、つまりsane indexingと表示されます)。残念ながら、この解決法はあまり効果がありません。私の 'main.m'ファイル(そのスクリプトを含んでいる)をダーティにした場合、実行します。***" java.lang.Exception:java.lang.RuntimeException:デバッグ中にmain.mに保存できません。モードを再試行してください。 "*** –

+0

@Piインデックス作成の問題を指摘してくれてありがとう。あなたはデバッグモードに入っていると言いますが、その場合ですか?あなたはデバッグモードではないときにそれを実行しようとしましたか? – Suever

関連する問題