I have coded a model that contains several parameters. The parameters can be changed interactively by the user using an interface contained in a DynamicModule[]. There are several "basic states" of the model, i.e. several sets of parameters that can be chosen as default. The user can pick the defaults by pressing appropriate buttons in the interface. For example:

  defaultParameters = {Hold[a=1;b=1;],

  Button["Default 1",ReleaseHold[defaultParameters[[1]]]]
  Button["Default 2",ReleaseHold[defaultParameters[[2]]]]

At several places in the code, I take advantage of the set of parameters being "saved" in the variable defaultParameters, as it is quite convenient to set all parameters in a single statement: ReleaseHold[defaultParameters[[n]]]. In reality, there are many more parameters than just a and b.

Now to my question:

When the user is changing the model interactively, I want him to able to save the current state of the model as another default set. However, I do not know the correct combination of Hold, Evaluate, etc to make it work.

I want something like

Button["save parameters",userParam = Hold[a=Evaluate[a];b=Evaluate[b];]]

but this doesn't do the trick. In the end, I wish to be able to call the parameters with ReleaseHold[userParam].


2 Answers 2


To save and restore data, you could use a Button for saving and an ActionMenu for restoring. The defaultParameters could just be the data -- you have to remember what order you put things, but usually that is important only in two or three places (the Button, the ActionMenu, and your case initializing defaultParameters). Merely for demonstration purposes I added some controls so that one could play with a and b.

DynamicModule[{a, b, defaultParameters = {{1, 1}, {2, 2}}},
   Dynamic @ ActionMenu[
     "Restore parameters...", # :> ({a, b} = #) & /@ defaultParameters], 
   Labeled[InputField[Dynamic @ a, Number, FieldHint -> "enter a"], "a", Left],
   Labeled[InputField[Dynamic @ b, Number, FieldHint -> "enter b"], "b", Left],
   Button["Save parameters", AppendTo[defaultParameters, {a, b}]]

Mathematica graphics

Note that Button has the Attribute HoldRest, so some of your use of Hold seems unnecessary. If you wish to have the default buttons as in your question, then they can be incorporated in the Column above in this form:

Button["Default 1", {a, b} = defaultParameters[[1]]],
Button["Default 2", {a, b} = defaultParameters[[2]]],

If you want, you could add a button to reset the defaults:

Button["Reset saved parameters", defaultParameters = {{1, 1}, {2, 2}}]

In the meantime, I have found a solution. I am using the following construct:

userParam = With[{tmp={a,b},Hold[{a,b}=tmp]]

Better suggestions are appreciated, but this solves my immediate question.

  • $\begingroup$ How would you save а variable that is declared as a function, say a[x_]:=x^2 ? Note that it also can depend on some local variables of the DynamicModule. $\endgroup$ Sep 19, 2013 at 23:53

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.