2
1
I want hide all outputs created by Print command from user's function defined in module .m
.
More precisely the problem is following:
I have a module that solves some specific task (It's not necessary to know the task for understanding the question). I use Print command inside the module to output process of evaluation. It's useful for debugging but not for using (all inner output must be hidden when user calls a function declared in the module).
So, in every stable version of the module I have to put comments (* *
) and delete them for gebugging. I suppose that this way is uncivilized, and Mathematica should have a regular way to hide inner outputs.
For example, we have 3 files in the same directory. Can you suggest me a simple way how to hide all outputs by default and print all outputs in debugging mode?
modul.m
:
f[x_]:=Module[{y}, y=Cos[x]*Sin[x];
Print["modul.m: y=",y];
Return[y]];
debugging.nb
:
SetDirectory[NotebookDirectory[]];
<< modul.m;
x=10;
f[x] (* print all inner output generated while f is evaluating*)
x=20;
f[x](* print all inner output generated while f is evaluating*)
application.nb
:
<< modul.m
y=f[10]+f[20];
(* Hide all outputs of f during evaluation. Command ; doesn't work for this issue. *)
Thank you very much for your answer! I chose first solution (by Brett Champion's) since it can work with multiple argument in debugPrint
debugPrint["modul.m: y=", y];debugPrint["modul.m: y=", y,"z=",z];
But the idea of using Message looks great. I wish there was posibility to define debugPrint with apriory unknown number of arguments. – Anonimous – 2012-01-11T07:37:41.047@Anonimous When using the
Message
version ofdebugPrint
, you can either put everything in a list before you pass it to it,debugPrint[{1,2,3}]
, or we could do something likedebugPrint[args__] := Message[debugPrint::msg, StringJoin[ToString /@ {args}]]
to emulatePrint
(unlikePrint
, this loses formatting, so you can't use it with e.g.Graphics
!), ordebugPrint[args__] := Message[debugPrint::msg, StringJoin[Riffle[ToString /@ {args}, ", "]]]
to have comma separated arguments. Preserving formatting is also possible, but it's more advanced so I won't go into it. – Szabolcs – 2012-01-11T07:58:34.883@Anonimous I just realized that
StringForm
is nestable, so you could use the message baseddebugPrint
like this too:debugPrint[StringForm["x = ``, y = ``", x, y]]
(or maybe integrateStringForm
directly into the function) – Szabolcs – 2012-01-11T23:22:36.230