MATLAB: Store which iterations that the error took place

erroiterationsliststore

Hi everyone, I've been using try,catch and continue in my loop for any error. I was wondering is there any code that captures which iteration that error took place in the loop? so far I've put catch ME; disp(iter) to show me which iterations it is that the error took place but I can't capture them

Best Answer

You could customize your own error message by basing it on the actual error message and appending the iteration number.
for i =1: 10
try
% [code that you don't trust :D]
catch ME
fprintf('%s Iteration #%d.\n', ME.message,i) %display error msg & iteration #
% or
error('%s Iteration #%d.', ME.message,i) %this will break the code
end
end