store which iterations that the error took place

4 Ansichten (letzte 30 Tage)
JL
JL am 29 Aug. 2019
Kommentiert: JL am 30 Aug. 2019
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

Akzeptierte Antwort

Adam Danz
Adam Danz am 29 Aug. 2019
Bearbeitet: Adam Danz am 29 Aug. 2019
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 Kommentare
Adam Danz
Adam Danz am 30 Aug. 2019
badIterations = [];
for i =1: 10
try
% [code that you don't trust :D]
catch ME
badIterations(end+1) = i;
end
end
JL
JL am 30 Aug. 2019
Thanks Adam!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 29 Aug. 2019
Bearbeitet: KALYAN ACHARJYA am 29 Aug. 2019
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 Kommentar
JL
JL am 30 Aug. 2019
Thanks but It can't seem to work. I want to store each iterations value into a maxtrix

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by