1
$\begingroup$

I have some fundamental misunderstanding of the use of Mma's GUI elements, and I would appreciate some guidance. I would like to be able to set up a calculation based on slider values and then press a button to do the calculation. The calculation may be lengthy, so I do not want it dynamically updated. I think (?) I can illustrate the issue with a plotting example that does not work. Can I set the parameter in a slider but only update the following plot when I press a button?

ClearAll[h, k]
h[k_] := Plot[Sin[k x], {x, 0., 2. Pi}, PlotLabel -> Sin[k x]];
DynamicModule[{k = 1},
 Column[{h[Dynamic@k], 
   Row[{Slider[Dynamic@k, {Range[10]}], Dynamic@k, Button["plot", h[k]]}, Spacer[2]]}]]

I hope this failed attempt will highlight my misunderstanding of the core issues, as I would like to receive some conceptual guidance. I usually find the Mma docs amazingly helpful, but not in this case.

$\endgroup$
6
  • $\begingroup$ Who is the audience for this GUI? Is it enough that the button effects a Shift+Enter so it evaluates a cell for instance? Some more info on the use-case would be nice. $\endgroup$ May 27 at 13:06
  • $\begingroup$ @MariusLadegårdMeyer I don't think the context matters much, but consider a notebook of simulation models for students to experiment with interactively. Functions will be defined globally but the model parameters should be set (via a GUI) in an isolated (per model) way. (Thus the DynamicModule wrapping the controls.) The important thing is to be able to change the parameters before initiating the computation. See typical NetLogo models for illustrative examples. $\endgroup$
    – Alan
    May 27 at 13:41
  • 1
    $\begingroup$ While I think its a good idea to learn more about Dynamic, here is solution using built-in options to Manipulate: Manipulate[h[k], {k, 1, 10}, ContinuousAction -> None]. The key piece is ContinuousAction. $\endgroup$
    – chuy
    May 27 at 19:36
  • $\begingroup$ @chuy That's great, but (iiuc) it won't meet my needs, since there will be multiple sliders to set before the expression should be evaluated. Thanks! $\endgroup$
    – Alan
    May 27 at 19:57
  • 1
    $\begingroup$ It should behave the same way with multiple controls. It won't update until the 'U' button is pressed. $\endgroup$
    – chuy
    Jun 1 at 15:58
1
$\begingroup$

Let the button update a variable: "kk", that will govern the plot:

ClearAll[h, k, kk]
h[k_] := Plot[Sin[k x], {x, 0., 2. Pi}, PlotLabel -> Sin[k x]];
DynamicModule[{k = 1}, 
 Column[{Dynamic@
    h[kk], {Row[{Slider[Dynamic@k, {Range[10]}], Dynamic@k, 
      Button["plot", kk = k]}, Spacer[2]]}}]]

Before clicking "Plot"

enter image description here

After clicking "Plot":

enter image description here

$\endgroup$
5
  • $\begingroup$ Cool. If I move kk into the module variables this does what I want. I had focused on applying dynamic to variables, not to compound expressions. Can you elaborate on the meaning of "displayed form" in the docs for Dynamic? $\endgroup$
    – Alan
    May 27 at 15:30
  • $\begingroup$ "displayed form" means what you see on screen, not the actual code that you can get with "FullForm" $\endgroup$ May 27 at 17:13
  • $\begingroup$ Yeah, I got that. But I don't know how to interpret it in the context of the docs. I think the relevant text to improve my understanding is rather this: "Dynamic[expr] evaluates expr whenever it needs its value and it determines that any type of value of symbols on which it depends might have changed." $\endgroup$
    – Alan
    May 27 at 20:01
  • $\begingroup$ In that documentation, however, the phrase "whenever it needs its value" is mysterious to me. $\endgroup$
    – Alan
    May 27 at 21:22
  • $\begingroup$ My interpretation: when it needs the value and in addition, it notes that the value has changed. $\endgroup$ May 28 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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