How to make Mathematica print to console warning messages during running code?
One of the hardest things for me to debug, is when Mathematica says that it detected a problem, but it does not print to the console a message at the time it did.
Like this diagram
The way I debug is by Print messages. I have different level of debug messages and can turn them on and off when there is a problem to help me find the location. All my debug message go to the console.
Since Mathematica does not print to the console these warnings/errors, it is not possible to find where this message came from. If it did, then I would know, because I can see my own messages before it, and know from where and when this error happened.
Clicking on the little '+' to see the error is after the fact, and it does not tell the location of the message.
I went to Edit/Preferences, and have everything t开发者_高级运维urned on to print to console that I can see:
btw, this particular 'error' is (when I click on '+')
InterpolatingFunction::dmval: Input value {0} lies outside the range
of data in the interpolating function. Extrapolation will be used. >>
It prints to console only when I click on the '+' by hand. I want this be done automatically when it happens.
So, my question is, how to make Mathematica print the above message to console, right when it happens?
ps. I do not know how to use WB to debug manipulate code.
thanks,
EDIT 1
I found the error, it was a typo in a name of a variable. If someone wants to see it, I put the notebook itself, and the error back in. I am sorry that I am not going to try to duplicate this in small program but this notebook will illustrate the issue in this folder there is the notebook (.nb) if someone wants to see it.
The error is that I wrote, in the module called 'solve[]
' above half way down the notebook, in the line where I set the local variables for this function, the following
Module[{numericalSolution, ic, t, ndsolveOptions, x1, x2, x3, x1de,
the last variable above should have been 'x1der' and not 'x1de' as in:
Module[{numericalSolution, ic, t, ndsolveOptions, x1, x2, x3, x1der,
fixing this, caused the error to go away.
Simply run the notebook, when it comes up, you'll see the error on the UI cell bracket, but no error on the console. (Program not complete, but works, when done, will be on my web site Mathematica demonstrations page, it is a simulation of triple pendulum).
Question again, how to make Mathematica print this error to console at the time when it detects it.
thanks.
From the screen shot it looks like you are using Manipulate. The reason for Manipulate (and other Dynamic construct) having red brackets to hide warnings and errors is that these messages can occur many many times while interacting, making the frontend unresponsive.
To answer your question about debugging, here is one possible approach. Consider this simple Manipulate which will throw a warning when you move the slider to x=0:
Manipulate[1/x, {{x, 1}, -3, 3, 1}]
To understand the message, you can click on the top-right circled plus sign and select 'Paste Snapshot'. This will paste a snippet of code that is equivalent to the code in the Manipulate with those parameter settings. In this case I get:
DynamicModule[{x = 0}, 1/x]
I can evaluate this and observe the Power::infy message in the notebook. In this particular example it is now clear what the problem is. In more complicated examples you can use the snap shot as a starting point to figure out what the problem might be.
精彩评论