repeat the iteration with an error using try/catch
조회 수: 7 (최근 30일)
이전 댓글 표시
I wrote a nested for loop to exexcute a huge number of iterations. However, sometimes an error stops the program. I want to repeat the iEv'th iteration in case an error occurs and then continue to next iteration. Please see below the code and let me know if it should be modified.
for iTcm=1:nTcm
for iScen=1:nScen
for iEv=1:nEv
try
MyProgramHere
catch ME
disp(ME);
fprintf('Retrying the program...\n');
end
end
end
end
댓글 수: 0
채택된 답변
James Tursa
2020년 4월 3일
편집: James Tursa
2020년 4월 3일
Maybe this construct does what you want
while( true )
try
MyProgramHere
break
catch ME
disp(ME);
fprintf('Retrying the program...\n');
end
end
댓글 수: 3
James Tursa
2020년 4월 3일
편집: James Tursa
2020년 4월 4일
No. I meant exactly what I wrote, without this line:
iEv = iEv - 1; % delete this line
By getting rid of this line, it will keep repeating the same iteration indefinitely until it passes. iEv doesn't change until you do the iteration successfully. If you want a limit on the number of tries before you generate an error then additional code would need to be added.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!