开发者

Manipulate updating the expression when a variable changes which is not on its TrackedSymbols

开发者 https://www.devze.com 2023-04-02 19:01 出处:网络
I gave up on this one. May be someone can see something obvious. I have a trigger which updates variable \'time\' when clicked on.

I gave up on this one. May be someone can see something obvious.

I have a trigger which updates variable 'time' when clicked on.

Next, I set TrackedSymbols->{time}, expecting the expression of the Manipulate to be re-evaluated only 'time' changes.

But I find that by changing another variable, which is NOT in the TrackedSymbols list, the expression is also being updated.

Here is an example

Manipulate[
 (
  If[time == 0, n = 0];
  Print[DateString[]];
  n
  ),
 {{time, 0, "run"}, 0, 20, AnimationRate -> 1, ControlType -> Trigger},
 {{n, 0}, None},

 TrackedSymbols -> {time}
 ]

The above wo开发者_JAVA技巧rks AS EXPECTED. when I click the run, click Pause, then click reset, all works ok, expression is only updated when trigger is 'running'.

Now I add this one line, so the above becomes

Manipulate[
 (
  If[time == 0, n = 0];
  Print[DateString[]];

  n=n+1;     (* ADD THIS *)

  n
  ),
 {{time, 0, "run"}, 0, 20, AnimationRate -> 1, ControlType -> Trigger},
 {{n, 0}, None},

 TrackedSymbols -> {time}
 ]

Now, I do the same as before, click 'run', followed by pause then reset. Expected the expression NOT to update since the trigger is reset. i.e. the 'time' variable is not changing.

But after 3-4 seconds of hitting the reset, the expression starts to update by itself, with the right side of the cell showing it is busy, this is without me hitting the run button or touching anything. The magic of Dynamics at work :)

Manipulate updating the expression when a variable changes which is not on its TrackedSymbols

Next, I moved the variable 'n' down to the Initialization section, and now the problem went away:

Manipulate[
 (
  If[time == 0, n = 0];
  Print[DateString[]];
  n = n + 1;
  n
  ),
 {{time, 0, "run"}, 0, 20, AnimationRate -> 1, ControlType -> Trigger},

 TrackedSymbols -> {time},
 Initialization :>
  (
   n = 0;
   )
 ]

Now it will not update on its own as before.

My question is: Why when 'n' is written as {{n, 0}, None} there is the above problem with Manipulate? I always thought by using TrackedSymbols, only those symbols when changed value will Manipulate update the expression.

I am sure there is a simple reason for this, but after an hour on this, I can't see it. Sometimes I think that I will never fully understand Mathematica Dynamic[].

Thanks for any explanation.

Update:

I think the problem is in the Trigger itself. I must be not understanding how Trigger works. Clicking 'reset' on the trigger does not seem to stop it from ticking? I am really confused now.


Try TrackedSymbols :> {time}.

0

精彩评论

暂无评论...
验证码 换一张
取 消