필터 지우기
필터 지우기

How to use try/catch so that my program rerun until it correct ?

조회 수: 25 (최근 30일)
Dear Matlab users,
I need to process a large amount of data using parfor and I am running into a problem where my program can sometime success, sometime not success
parfor i = 1:100
MyProgram
end
Is there anyway to utilize the try/catch statement so that everytime when my progragram run into some kind of error, the worker will
1/ Retry untill success
2/ Put a warning about the iteration i and then abandon the current job to move on
Thank you very much

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 16일
niters = 100;
status = logical(niters, 1);
message = cell(niters, 1);
parfor i = 1 : niters
blah blah blah
try
something
status(i) = true;
message{i} = [];
catch ME
status(i) = false;
message{i} = ME;
end
end
  댓글 수: 6
Walter Roberson
Walter Roberson 2020년 7월 16일
You do not have to keep track of retries; you could just let it retry indefinitely. That would make the code a bit more simple.
niters = 100;
parfor i = 1 : niters
blah blah initialization
while true
blah blah anything that needs to be reset every retry
try
something
break; %you did not fail, so you can stop retrying.
end
end
end
Perhaps it might be more clear to you as
niters = 100;
parfor i = 1 : niters
blah blah initialization
failed = true;
while failed
blah blah anything that needs to be reset every retry
try
something
failed = false;
end
end
end
Tuong Nguyen Minh
Tuong Nguyen Minh 2020년 7월 17일
Oh thank you very much, I did not know that this try catch command would even be that flexible. Your explanation is excellent and the code organization is very thoughful !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by