Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have some Mathematica code that analyzes data in a database and uses Message to report when expected data is missing. By default, Mathematica will allow this message to appear only three times before it gets shut off, but I'd like this message to appear as often as the errors do.

The solutions I can think of are

Off[General::stop];

but this kills limits for all error messages, not just this one,

or just to switch to Print, which seems inelegant.

Is there some way to allow this one message to appear more than three times, while leaving behavior unchanged for the rest?

share|improve this question
1  
Perhaps Assert would be useful for this? –  Brett Champion Feb 29 '12 at 19:24
add comment

3 Answers

up vote 10 down vote accepted

You can do something like this:

resetMessages[symbol_] := 
 With[{mysymbol = symbol}, 
  Unprotect[$MessageList]; $MessageList = 
   DeleteCases[$MessageList, HoldForm[MessageName[mysymbol, _]]]; 
  Protect[$MessageList];]

And you will have to call it after each function...

Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; \
Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; \
resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; \
Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; \
Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; \
resetMessages[Exp];

Sqrt::argx: Sqrt called with 4 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Sqrt::argx: Sqrt called with 4 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Sqrt::argx: Sqrt called with 4 arguments; 1 argument is expected. >>

General::stop: Further output of Sqrt::argx will be suppressed during this calculation. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>
share|improve this answer
    
I independently arrived at $MessageList as well, but I used it a little differently. See my answer below. Also, +1. –  Mr.Wizard Feb 29 '12 at 19:34
add comment

Why don't you use Check. You could combine it with Reap/Sow or Throw/Catch. Here's an example that counts divisions by zero.

SeedRandom[1];
list = RandomInteger[{-2, 2}, {100}];
Quiet[results = Reap[Table[Check[1/x, Sow["bad news"]],
  {x, list}]]];
Length[results[[2, 1]]]

Of course, since the second argument of Check can be anything, you could arrange for it to alert you any number of ways.

share|improve this answer
add comment

New Method

FJRA pointed out that my original method will fail in certain cases. Here is what I hope is a more robust approach:

Unprotect[Message, $MessageList]

Message[args___] /; ! TrueQ[$msgClear] :=
 Block[{$msgClear = True},
   $MessageList = DeleteCases[$MessageList, HoldForm[Power::infy]];
   Message[args]
 ]

Now the specified message will print an infinite number of times:

Table[1/0, {7}]

You could use Alternatives in DeleteCases to make multiple messages infinite.


Old Method

I also came up with tinkering with $MessageList as did FJRA, but my application is a little bit different:

Unprotect[$MessageList];

$MessagePrePrint =
  ($MessageList = DeleteCases[$MessageList, HoldForm[Power::infy]]; #) &;
share|improve this answer
1  
This is more elegant, the only problem I see is that $MessagePrePrint is only used when the message has arguments like 1, 2. If the message is only a string it doesn't call $MessagePrePrint. But in general it avoids the "manual" reset, which is cleaner. –  FJRA Feb 29 '12 at 20:02
    
@FJRA I didn't realize that; thank you. Please see up updated answer and tell me if you can see any specific problems with my new method. –  Mr.Wizard Feb 29 '12 at 20:19
    
I think that's the best way to clear the message list, because calling extra functions is not needed. –  FJRA Feb 29 '12 at 20:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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