store which iterations that the error took place

조회 수: 9 (최근 30일)
JL
JL 2019년 8월 29일
댓글: JL 2019년 8월 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

채택된 답변

Adam Danz
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
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
JL
JL 2019년 8월 30일
Thanks Adam!

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

추가 답변 (1개)

KALYAN ACHARJYA
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
  댓글 수: 1
JL
JL 2019년 8월 30일
Thanks but It can't seem to work. I want to store each iterations value into a maxtrix

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by