store which iterations that the error took place
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Adam Danz
2019-8-29
编辑:Adam Danz
2019-8-29
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
4 个评论
Adam Danz
2019-8-30
badIterations = [];
for i =1: 10
try
% [code that you don't trust :D]
catch ME
badIterations(end+1) = i;
end
end
更多回答(1 个)
KALYAN ACHARJYA
2019-8-29
编辑:KALYAN ACHARJYA
2019-8-29
l=1;
error_iter=[];
for i=1:iter
% do operation
if error %error condition
error_iter(l)=iter
l=l+1;
end
end
error_iter
另请参阅
类别
在 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!