5
$\begingroup$

I have a complicated function for StreamPlot to call that includes some error checking and the use of Message. Somehow the messages are not displayed when the function is called from StreamPlot. Here's a toy example:

Define the function & message:

f[x_] := Module[{},
  If[x > 1, Print["print: ", x]; Message[f::msg, x]];
  x];

f::msg = "`1` is greater than 1";

Test it by itself:

f[1.2]
(* print: 1.2 *)
(* f::msg f:1.2` is greater than 1 *)
(* 1.2 *)

Works as expected, but put it in a StreamPlot and only the Printed messages are shown:

StreamPlot[{f[x], y}, {x, 0, 1.2}, {y, 0, 1}]
(* txt: 1.22403 *)
(* txt: 1.22403 *)
(* ... *)

How can I get those Messages to show?

Update:

Plot eats Messages too.

$\endgroup$
  • $\begingroup$ Oddly, it does not eat one of the messages here. $\endgroup$ – Michael E2 Mar 27 '17 at 19:09
2
$\begingroup$

You need to reset $Messages.

f[x_] := Module[{}, 
   If[x > 1, Print["print: ", x]; 
    Block[{$Messages = Streams["stdout"]},
     If[! ListQ@$MessageList, $MessageList = {}];
     Message[f::msg, x]]];
   x];

$MessageList should be a list, which if is not when f is called. Just what to do about it, whether to Block it too or reset it globally as above, I am not completely sure. It worked both ways with the following plot:

Plot[f[x], {x, 0, 1.01}]
$\endgroup$
  • $\begingroup$ Thanks! Seems like I will need to replace a lot of calls to Message with something like this. Could this be wrapped into a plug-and-play function with identical syntax to Message? Also, would you consider this a bug to report to WRI? $\endgroup$ – Chris K Mar 27 '17 at 20:16
  • $\begingroup$ In response to my first question in the previous comment, seems so. Will add as a separate answer later tonight. $\endgroup$ – Chris K Mar 27 '17 at 21:53
0
$\begingroup$

Here's my attempt to wrap up @MichaelE2's trick into a plug-and-play replacement for Message:

Msg[msgs__] := Block[{$Messages = Streams["stdout"]},
  If[!ListQ@$MessageList, $MessageList = {}];
  Message[msgs];
];
SetAttributes[Msg, HoldAll];

This seems to work except it usually overrules General::stop in Plot and StreamPlot, which I can live with.

$\endgroup$

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.