store which iterations that the error took place
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Adam Danz
le 29 Août 2019
Modifié(e) : Adam Danz
le 29 Août 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 commentaires
Adam Danz
le 30 Août 2019
badIterations = [];
for i =1: 10
try
% [code that you don't trust :D]
catch ME
badIterations(end+1) = i;
end
end
Plus de réponses (1)
KALYAN ACHARJYA
le 29 Août 2019
Modifié(e) : KALYAN ACHARJYA
le 29 Août 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
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!