How to automatically continue running the program after an error.

50 ビュー (過去 30 日間)
Theo
Theo 2014 年 9 月 24 日
編集済み: Theo 2014 年 9 月 24 日
The problem I'm having is currently with the GMM fit model. but it's rather general. during the run of my program GMM sometimes gives the error of ill conditioned matrix and breaks the code. Now my code is in a long loop with a counter. what I'm doing right now is to manually reset the loop counter in the code body to the last working number, so that the code restarts from the point where it stoped. But it's tedious. Is there a way to tell matlab to do a few basic operations in case of an error and try again, instead of breaking off with an error?

採用された回答

Guillaume
Guillaume 2014 年 9 月 24 日
Use a try catch to catch the error and respond appropriately to it:
counter = 1;
while counter <= num_loop %if you want to retry the loop, you need to change it into a while loop
%... some code
try
%some operation that may terminate
catch err
if strcmp(err.identifier, 'whatever identifier is thrown by GMM')
counter = counter - 1; %retry loop
else
rethrow(err); %some other unexpected error. Better stop
end
end
counter = counter + 1;
end
  1 件のコメント
Theo
Theo 2014 年 9 月 24 日
編集済み: Theo 2014 年 9 月 24 日
Thanks it works :)

サインインしてコメントする。

その他の回答 (1 件)

Alberto
Alberto 2014 年 9 月 24 日
You should try to catch the error using the structure try/catch structure:
try
statements
catch exception
statements
end
Use Product Help for details

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by