MATLAB: How to keep a for loop going after error

for loop

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)

Best Answer

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