Message Boards Message Boards

0
|
5371 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Avoid to evaluate cells twice for the initialization of a Manipulate?

Posted 7 years ago

This manipulate program below allows the user to enter points on a LocatorPane and then draws a line through the points. enter image description here

The Manipulate has an Initialization, where the programmer can define initial values for the points. The initial values are the variable ipts in the code below. What I observe is that the definition of the Manipulate must be evaluated twice before the modified values are displayed. Suspect that I am misunderstanding some fundamental concept about how initialization works. Could someone point out how to revise the code?

On a possibly related note, what must be done so that the variables, ipts, xMin and xMax in the code below have a scope local to the Manipulate?

Steps to reproduce the problem:

  1. Action: evaluate the cell; Response: the manipulate will appear
  2. Action: edit the code; change the value of ipts (e.g. change the first point to {0.15, 0.05}
  3. Action: evaluate the cell; Response, the manipulate will appear, the first point will have the original value {0.15, 0.35}
  4. Action: evaluate the cell; Response: the manipulate will appear, the first point will have the revised value {0.15, 0.05}

Question: what must change so that manipulate will display the revised value after step 3?

myTest2 = Manipulate[
   (*User points*)
   posSorted = Sort[pos];
    xMin = Min[posSorted[[All, 1]] ];
   xMax = Max[posSorted[[All, 1]] ];
   gvfuncUser = Interpolation[posSorted, InterpolationOrder -> 1];
   myPlot = 
    Plot[gvfuncUser[x], {x, xMin, xMax}, 
     PlotRange -> {{0, 1}, {0, 1}}, Frame -> True, ImageSize -> 400];
   Grid[{
     {LocatorPane[Dynamic@pos, myPlot, LocatorAutoCreate -> True, 
       ContinuousAction -> False] }
     }]
   ,
   (*list of controls*)
   {{pos, ipts}, ControlType -> None}
   (*Initialization*)
   , TrackedSymbols :> {pos, ipts}
   , Initialization :> (
     ipts = {{0.15, 0.35}, {0.25, 0.15}, {0.50, 0.17}, {0.75, 
        0.18}, {1, 1}};
     posSorted = {}; (*do this so that posSorted is local to this Manipulate (?) *)
     gvfuncUser = {};
     )
   , SynchronousUpdating -> False
   ];
myTest2
Attachments:
POSTED BY: Robert McHugh
4 Replies
Posted 7 years ago

Variables can be localized to a Manipulate. By declaring them as Controls with ControlType->None. And excluding them from the list of TrackedSymbols.

myTest3 = Manipulate[
    (* Preamble *)
    posSorted = Sort[pos];
    xMin = Min[posSorted[[All, 1]]];
    xMax = Max[posSorted[[All, 1]]];
    gvfuncUser = Interpolation[posSorted, InterpolationOrder -> 1];
    myPlot = Plot[gvfuncUser[x], {x, xMin, xMax}, PlotRange -> {{0, 1}, {0, 1}},Frame -> True, ImageSize -> 400];
    (* The output *)
    Grid[{{LocatorPane[Dynamic@pos, myPlot, LocatorAutoCreate -> True, ContinuousAction -> False]}}],
    (* Control *)
    {{pos, ipts}, ControlType -> None},
    (* Local variables, as controls with ControlType->None *)
    {{ipts, {{0.15, 0.05}, {0.25, 0.15}, {0.50, 0.17}, {0.75, 0.18}, {1, 1}}}, ControlType -> None},
    {posSorted, ControlType -> None},
    {xMin, ControlType -> None},
    {xMax, ControlType -> None},
    {myPlot, ControlType -> None},
    {gvfuncUser, ControlType -> None},
    (* Manipulate options *)
    TrackedSymbols :> {pos}, SynchronousUpdating -> False
  ]
POSTED BY: Hans Milton

After evaluating the first time, modify the initial points, and the panel still shows the original set of points.

POSTED BY: Gianluca Gorni

Your panel seems to work fine for me at the first evaluation. You can wrap Manipulate into a DynamicModule to localize variables.

POSTED BY: Gianluca Gorni
Posted 7 years ago

After evaluating the first time, modify the initial points, and the panel still shows the original set of points.

POSTED BY: Robert McHugh
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract