필터 지우기
필터 지우기

How to keep a for loop going after error

조회 수: 36 (최근 30일)
Joe
Joe 2016년 2월 27일
댓글: Joe 2016년 3월 6일
Hi,
I need to do a calculation that involves a for loop. In each iteration the for loop calls a function, and this function sometimes crashes. The fact that the function crashes sometimes it's expected and unavoidable.
The problem is that after the crash of the function, the for loop stops and I have to manually restart.
Is there a way to keep the for loop going after the error / crash (e.g. If the for loop needs to do 500 iterations and crashes in iteration 127, it just continues with iteration 128)

채택된 답변

Stephen23
Stephen23 2016년 2월 27일
편집: Stephen23 2016년 2월 27일
doc try
Simply put in a try:
for k = 1:N
try
... code that might cause an error
catch % optional!
... optional code to run if above code fails
end
end
  댓글 수: 2
Image Analyst
Image Analyst 2016년 2월 27일
Exactly what I was going to say (so you earned a "vote" from me). Note that if you don't want to do anything, you can just put "continue" in the catch block to skip to the bottom of the for loop, or you can leave it totally empty (no lines at all in the catch block) in which case it will carry on with the for loop's code immediately after the "end" statement of the try/catch block.
Joe
Joe 2016년 3월 6일
Thanks both, works very well!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by