です:
function errorIndexes = myScript(indexes)
errorIndexes = [];
errors = {};
for i = indexes
try
%Do something
catch me
errorIndexes(end+1) = i;
errors{end+1} = me;
end
end
end
は、外側には、あなたがそのようなメインファイルを持っている必要があります。私は、マシンがいつでも使用できなくなる環境で動作する長時間実行(1日以上)のループと同様のものを使用しました。研究室でスペアマシンの分散クラスタ。
基本的には、ループを開始する前にチェックポイントファイルが存在するかどうかをチェックします。それ以上の場合、ループは前回正常に終了しませんでした。これには、ループがどこに到達したのか、また、もう一度やり直す必要がある他の状態に関する情報が含まれています。
function myFunction()
numIter = 10;
startIter = 1;
checkpointFilename = 'checkpoint.mat';
% Check for presence of checkpoint file suggesting the last run did not
% complete
if exist(checkpointFilename, 'file')
s = load(checkpointFilename);
startIter = s.i;
fprintf('Restarting from iteration %d\n', startIter);
end
for i = startIter:numIter
fprintf('Starting iteration %d\n', i);
expensiveComputation();
save(checkpointFilename, 'i');
end
% We succefully finished. Let's delete our checkpoint file
delete(checkpointFilename);
function expensiveComputation()
% Pretend to do lots of work!
pause(1);
end
end
実行とこのようになりますを通じてCtrl + Cキーの途中で飛び出し:
>> myFunction
Starting iteration 1
Starting iteration 2
Starting iteration 3
Starting iteration 4
Operation terminated by user during myFunction/expensiveComputation (line 27)
In myFunction (line 18)
expensiveComputation();
>> myFunction
Restarting from iteration 4
Starting iteration 4
Starting iteration 5
...
は、今私は、同様のテンプレートを持っている
は、ここで簡単な例です。しかし、エラーが発生したとき@ iter = 5、私はそれを修正し、iter = 5:nから(変更された)スクリプトを再度実行します。この場合はiter = 5から再起動するスクリプトを変更しないで、何らかの方法がありますか? – Learner
多分私ははっきりしませんでした。これはスクリプトを変更しません。コマンドラインで入力するだけです。 – Oli