MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Tracking a dynamic variable

  • To: mathgroup at
  • Subject: [mg88622] Re: [mg88607] Tracking a dynamic variable
  • From: John Fultz <jfultz at>
  • Date: Sat, 10 May 2008 06:51:54 -0400 (EDT)
  • Reply-to: jfultz at

One of the optimizations of the Dynamic system (and a very good one if you 
consider it for a while) is that no Dynamic will update unless it's visible.  In 
the case of TabView, this means that no tab will update unless that tab is 
presently selected.  Since Dynamic is generally about enabling interactivity on 
an area of your screen, it's a good thing not to spin CPU cycles for things
which aren't presently being displayed.

I'm uncertain how much to read into the details of your example to see how 
clever you were trying to be.  Possibly, you thought that by leaving flag1 
unscoped by anything except the outer Dynamic, thereby forcing the entire 
TabView to reevaluate, you would force the undelayed rule for "task 2" to 
evaluate.  Well, that does happen, but Dynamic of anything merely evaluates to 
itself.  I.e., if you evaluate...


it evaluates *not* to 4, but to Dynamic[2+2].  The computation which returns 4 
does not happen until the Dynamic is displayed to the screen.  Since, in your 
example, the second tab is never displayed to the screen, the computation in the 
Dynamic never happens.

Also, your example as at least one bug in it...

Dynamic[x=y; If[x!=y, flag1=True]]

will virtually never set flag1 to True because the If[] check always evaluates 
immediately after the x=y.  I would have expected something more like this,

Dynamic[If[x != y, y=x; (* do work *)]]

I'm still guessing as to your exact intent, but I think you basically have two 
options here...

* Restructure the code in the Dynamic so that everything can occur in a single 
Dynamic.  It's not clear to me from your example that this is impossible.
* Use a second Dynamic whose output is invisible...the Dynamic is onscreen, but 
has no apparent visual effect.

The invisible Dynamic technique would look something like this...

Row[{Dynamic[(* real output *)], Dynamic[ (* some computations *); ""]}]

So the Dynamic does get displayed to the screen, and updated every time the
first Dynamic is, but what actually gets displayed is an empty string, and 
therefore not noticeable.

John Fultz
jfultz at
User Interface Group
Wolfram Research, Inc.

On Fri, 9 May 2008 07:17:08 -0400 (EDT), Matthias Gottschalk wrote:
> Hello
> I have a larger program in which I want to look if a value
> of a dynamic variable has changed.
> A short version of this program looks like this:
> Interpretation[{x = 1, y = 1, flag1 = False, flag2 > False},
> TabView[{
> "task 1" ->
> Dynamic[
> Block[{},
> (* store old value of x *)
> y = x;
> (* test if x changed *)
> If[x != y, flag1 = True];
> (* display input and some outout *)
> Panel[
> Grid[
> {{
> InputField[Dynamic[x]],
> Dynamic[x],
> Dynamic[y],
> flag1
> }}
> ]
> ]
> ]
> ],
> "task 2" ->
> Dynamic[
> Block[{},
> If[x == y,
> flag2 = True,
> flag2 = False
> ];
> Panel[
> Grid[
> {{x, y, flag1, flag2}}
> ]
> ]
> ]
> ]
> }
> ],
> Print["end"]
> ]
> I want to track the variable x and if it has changed in
> "task 1"  an appropriate action should be accomplished in
> "task 2".
> The program above works only if for the input in "task 1"
> the Return key is not pressed in the InputField. If I
> press Return in the InputField the whole "task 1" is
> executed and then y will be equal to x in any case.
> Now how can I track the value of x so that I can decide in
> "task 2" that x has changed?
> Matthias

  • Prev by Date: Re: Re: TreePlots and GraphPlots
  • Next by Date: Re: 2 domain PDE, NDSolve
  • Previous by thread: Tracking a dynamic variable
  • Next by thread: PlotLegend not working properly in my Mathematica 6