Re: How to find which variable caused the trigger in Manipulate[]

*To*: mathgroup at smc.vnet.net*Subject*: [mg104150] Re: How to find which variable caused the trigger in Manipulate[]*From*: John Fultz <jfultz at wolfram.com>*Date*: Wed, 21 Oct 2009 06:29:42 -0400 (EDT)*Reply-to*: jfultz at wolfram.com

On Tue, 20 Oct 2009 04:54:32 -0400 (EDT), Nasser M. Abbasi wrote: > > > "John Fultz" <jfultz at wolfram.com> wrote in message > news:hav105$47o$1 at smc.vnet.net... >> This looks to me like a bug, but one that is very simple to work around. >> Use >> Rule instead of RuleDelayed for the Refresh. I.e., >> >> Manipulate[ >> Row[{Dynamic[Refresh[r process["x "]; "", TrackedSymbols -> {x}]], >> Dynamic[Refresh[r = process["y "]; "", TrackedSymbols -> {y}]], >> Dynamic[Refresh[Text[r], TrackedSymbols -> {x, y}]]}], {x, 0, >> 1}, {y, 0, 1}, {r, "", "", ControlType -> None}, >> Initialization :> (process[s_String] := >> Module[{}, Text[StringJoin[s, DateString[]]]])] >> >> After a bit of experimentation, my advice is to completely avoid >> RuleDelayed >> with the TrackedSymbols option. >> >> Sincerely, >> >> John Fultz >> jfultz at wolfram.com >> User Interface Group >> Wolfram Research, Inc. >> > > Version 7, on windows XP. > > Hello, > > I am afraid there is a slight technical problem with the above approach, > which is preventing my demo from being accepted. > > I have been trying to solve it for the last day or so, but no luck. > Before I give up and recode the whole demo, once again, to use a more > standard approach, I thought I'll describe the problem found and see may > be there is a solution I am not seeing. > > This might be a 'bug' or might be a feature, I am not sure. > > Background: When using Manipulate, and then closing the notebook, and > opening it, Manipulate will remember the values of the control as they > were before closing the notebook. This way, the screen will look the same > way it was before closing the notebook. Try it on any Manipulate and > you'll see. Mathematica saves in the notebook the values of the control > variables. > > So if one has a slider "x", at say some value 7, and then they > close/reopen the notebook, then they will see the slider back at the same > position, ie. at 7. > > The problem with the solution we have now as shown above, is that this > does not work any more. Manipulate will not remember the slider position, > unless the slider happened to be the LAST one in the Row[{}] shown. > > This is a very simple example to illustrate: This is a Manipulate with 2 > sliders. > > Manipulate[Row[{ > Dynamic[Refresh[r = x; "", TrackedSymbols -> {x}]], > Dynamic[Refresh[r = y; "", TrackedSymbols -> {y}]], > Dynamic[Refresh[Text[r], TrackedSymbols -> {y, x}]]} > ], > {x, 0, 1, 0.01}, > {y, 0, 1, 0.01}, > {r, "", "", ControlType -> None} > ] > > The above works fine. But notice that the first entry in Row[{}] is for > the x control variable, and the second entry is for the y control > variable. > > Now if you move the "x" slider and note the position it is at. Now save > the notebook, close it, and reopen it. Notice that the "x" slider is NOT > at the same position it is in. It is back to zero. > > Do the same now, but with the "y" slider, and repeat the same process, > then you will see when you reopen the notebook that the "y" slider is at > the SAME position it was before closing the file. Why Manipulate > remembers the position of "y" and not for "x"? > > This is because the line which says Dynamic[Refresh[r = y; "", > TrackedSymbols -> {y}]] happened to be the last entry in Row[{}] where > the r variable is being set. > > Changing the order of the Row[{}] lines, changes the above behavior. > > So, I am trying to solve this problem. But I have no idea how Mathematica > does these things internally, so I have been doing lots of trial and= error > trying to find out why. But no luck yet. > > I need a way to use the same approach as above, but have Manipulate > remember ALL the sliders positions as they were before closing the file, > not just the last one in the Row[{....}]. > > It seems that Row[{...}] is not the right thing to use for this, I tried > Grid, Column, and nothing helped, or may be this is a bug? May be some= one > has any other ideas, please let me know. > > I can ofcourse recode the whole thing to use a pull-down menu and let the > user select the action to do, and avoid all this Dynamic[Refresh[...]]] > but I think that Mathematica should be able to support such a design for > the interface. > > Thanks, > --Nasser I can't reproduce this. In 7.0.0 and 7.0.1, your example perfectly remembers for me both the values of x and y, setting the sliders at exactly the right point upon reloading. Perhaps what you meant is that it's not remembering the value of *r* after you move the x slider, save, and reload. It does properly store the value of r, but once you reload the notebook, all of the Dynamics compute again. Since computing the Dynamics has the side effect of setting r, the stored value of r will get wiped out. This amounts to the same problem as I mentioned in my first response...in this example, whether r is initially set to the value of x or y is undefined. It's undefined because there's nothing in the Mathematica documentation which guarantees the order in which the Dynamics are evaluated. This is true both when you initially create it, and also when you load it again from disk (or copy and paste it, as well). It so happens that it's evaluating Dynamics left-to-right in the Row, but that's not behavior you should count on. So the setting of r (if that is, indeed, what you're seeing) is within the designed behavior, and not a bug. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc.