3
$\begingroup$

The scrap of code below [from Mathematica's Help on DynamicModules] generates a Pane of Locators, and a line showing the interpolation between the datapoints.

DynamicModule[{pts = {{0, 0}, {1, 1}, {2, 0}, {3, 2}}}, 
 LocatorPane[Dynamic[pts], 
             Dynamic[Plot[InterpolatingPolynomial[pts, x], {x, 0, 3}, 
                          PlotRange -> 3]
 ]]]

When the program is saved,and reloaded, the Plot persists with the then current values of the points.

Suppose, after reloading the file (but not reinitializing the DynamicModule), I want to use the values of the points (which I moved in interacting with the program before saving it). In otherwords, I'd like to be able to access a list of the modified points with, perhaps, new functions.

I realize I could just read out variables in the original function, store them, then load them as part of the initialization procedure when restarting a program a second time, but this in effect means running the original program history completely again. In complex programs, with lots of potential branches, this could be tedious and error prone.

Any solution? DynamicModule would seem to attempt to address this problem by saving variables, but, for example, in the above code scrap, I want to get numerical values of the point positions for use in other parts of the program, how would I do it?

$\endgroup$
0

2 Answers 2

2
$\begingroup$

If I understand correctly, you can use Setting:

enter image description here

$\endgroup$
1
  • $\begingroup$ That's exactly something I was searching for... Thanks. $\endgroup$
    – unlikely
    Commented Mar 1, 2016 at 12:31
2
$\begingroup$

Another way is to include a mechanism for saving the state of the DynamicModule in the code. For instance, here's a button that will print a cell that contains code to initialize a global variable to the value of pts:

DynamicModule[{pts = {{0, 0}, {1, 1}, {2, 0}, {3, 2}}}, 
 Column[{LocatorPane[Dynamic[pts], 
    Dynamic[Plot[InterpolatingPolynomial[pts, x], {x, 0, 3}, 
      PlotRange -> 3, ImageSize -> Medium]]], 
   Button["Save", 
    With[{p = pts}, CellPrint[ExpressionCell[Defer[points = p], "Input"]]]]}]]

Mathematica graphics

One could change the action to simply setting the global variable, in this way:

Button["Save", points = p]
$\endgroup$
1
  • $\begingroup$ Michael 's answer does work for the example given, and I appreciate the result. My actual problem is a bit more complicated in that I have about 10 graphs, each with about 10 locators, which ultimately are the variables of interest for a calculation. While I can (and it looks like, probably will) use this method, keeping track of a 10 lists and corresponding file[s] associated with this program seems a bit inelegant. @Kuba 's Setting idea works well in the example given (which is slightly different from my actual usage), but I can't (yet) make it work with my actual code. I'll update... $\endgroup$ Commented Nov 21, 2013 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.