Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}}}, 
             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?

share|improve this question
up vote 2 down vote accepted

If I understand correctly, you can use Setting:

enter image description here

share|improve this answer
That's exactly something I was searching for... Thanks. – unlikely Mar 1 at 12:31

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}}}, 
    Dynamic[Plot[InterpolatingPolynomial[pts, x], {x, 0, 3}, 
      PlotRange -> 3, ImageSize -> Medium]]], 
    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]
share|improve this answer
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... – user2238171 Nov 21 '13 at 23:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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