Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

DynamicModule[{a,b,defaultParameters,userParam},
  defaultParameters = {Hold[a=1;b=1;],
                       Hold[a=2;b=2,]}

  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].

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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}}},
 Column[{
   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}}]
share|improve this answer

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.

share|improve this answer
    
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. –  Vahagn Poghosyan Sep 19 '13 at 23:53

Your Answer

 
discard

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.