I'm using NonLinearModelFit on a large set of data and which contains a nested Do-loop. I'd like to see all of the cases where there is an issue with the non-linear model fit, but after about five of these messages, further warnings are suppressed. Is there some way to make sure it doesn't suppress these messages after repeated warnings along the way?

z = NonlinearModelFit[trimspectra, a (x/x0)^(-b + c (Log[x/x0])), {a, b, c}, x];
  • 7
    $\begingroup$ From the docs on Message: Off[General::stop] makes the Wolfram Language not stop repeating messages. -- You might also be interested in Check. It can respond to messages even though the printing has stopped. -- Also perhaps of interest to you: mathematica.stackexchange.com/questions/20367/… $\endgroup$
    – Michael E2
    Mar 9, 2016 at 20:25
  • $\begingroup$ Yes, Thank you Michael, Message: Off[General::stop] did the trick. When I get a message, is there a simple way to print out a variable to tell me where in the do-loop the issue occurred? Such as If[MessageList[-1]!={}, Print["Variable = ", variableOfInterest] ] ? $\endgroup$ Mar 11, 2016 at 21:39
  • $\begingroup$ I just tried the If statement inside the loop. Outside the loop, it printed in a test case, but inside the loop, no printing. NonlinearModelFit::sszero: is my error warning. z = NonlinearModelFit[trimspectra, a (x/x0)^-b, {a, b}, x]; If[MessageList[-1] != {}, Print["LST = ", lst]]; $\endgroup$ Mar 11, 2016 at 21:48
  • 1
    $\begingroup$ The Q I linked to has a good method. See my answer below. $\endgroup$
    – Michael E2
    Mar 11, 2016 at 21:54

1 Answer 1


From Szabolcs's answer to How to catch complete error message information, including the message text as it would be printed?:

report[m_] := If[NumericQ[i], Print[HoldForm[i] == i]];
Internal`AddHandler["Message", report];

 1/Mod[i, 3],
 {i, 15}]

Internal`RemoveHandler["Message", report]

Mathematica graphics

One can also do the following if you know where the error might occur:

 Check[1/Mod[i, 3], Print[HoldForm[i] == i]],
 {i, 15}]

Mathematica graphics

  • $\begingroup$ Thanks again - the last section did it for me, but the 1st two sections are helpful in general for when I'd like to collect the messages. (-: $\endgroup$ Mar 11, 2016 at 22:09
  • $\begingroup$ @Szabolcs Sorry! $\endgroup$
    – Michael E2
    Mar 11, 2016 at 22:15
  • $\begingroup$ @MichaelE2 Don't worry about it, I know the spelling looks very strange :-) $\endgroup$
    – Szabolcs
    Mar 13, 2016 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.