2
$\begingroup$

I have a Mathematica notebook that includes some dynamic structures (with Manipulate). Sometimes, I discover that x has been assigned the value 0., but I don't know where this happened. I certainly never intended to do it. I suspect it is leaking from some dynamic structure.

Once I notice it, it's easy to clear the value, but I would like to figure out where this is coming from so I can fix it. What is the best way to figure out where it got assigned a value?

$\endgroup$
  • $\begingroup$ Why not do something like TracePrint[(* expr *), _Set | _SetDelayed]? $\endgroup$ – J. M. will be back soon Mar 16 '16 at 23:14
  • $\begingroup$ Now that x has been cleared, this only returns x (with no history). I'll try it out next time it unexpectedly has a value to see if it works then. $\endgroup$ – Seth Mar 16 '16 at 23:20
  • $\begingroup$ I don't think you can get history of usage of symbol. The new assignment erase the previous one and that is what you got. But you can create a function (at the beginning ) which gives you message when x is assigned to any value. $\endgroup$ – Algohi Mar 17 '16 at 1:40
  • $\begingroup$ Would the message say the line number where this happened? Or appear in the notebook right after the assignment? That would be enough. Why don't you write it as an answer and I'll accept it unless something better comes along. $\endgroup$ – Seth Mar 17 '16 at 1:45
  • $\begingroup$ I don't know what do you mean be line number ( I suppose MMA does not have lines numbers). The message will block the kernel at the step where x is attempted to be assigned. see the answer bellow. $\endgroup$ – Algohi Mar 17 '16 at 1:57
1
$\begingroup$

If you are looking particularly for x, you can create a message when x is attempted to be assigned a value for the first time:

Clear[x]
Set[x, v_] ^:= 
  DialogInput[
   Row@{CancelButton[], 
     DefaultButton[
      DialogReturn[OwnValues[x] = {HoldPattern[x] :> v}]]}];
$\endgroup$
  • $\begingroup$ I think that $NewSymbol and probably $NewMessage might be of interest, they provide functionality which should (also) make possible what you want... $\endgroup$ – Albert Retey Mar 17 '16 at 9:54
  • $\begingroup$ @AlbertRetey I thought of this also but this will not stop the kernel. This method will issue the message and the kernel will keep evaluation to the end $\endgroup$ – Algohi Mar 17 '16 at 14:33
  • $\begingroup$ OK, I understand now better what you intended adn that indeed seems to not be achieved easily with $NewSymbol. It probably is still good to have $NewSymbol mentioned in the comments to your answers, I think... $\endgroup$ – Albert Retey Mar 17 '16 at 14:57

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.