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 a DynamicModule with several graphic elements. I would like to create a button which would change a parameter and re-run the entire module.

Say that we have

h := Plot[Sin[k*x], {x, 0, 10}]

with an initial value of k = 1.

I want to create Button["Increase k", k = k + 1; h] that would run the plot (without using Print[h] as h is really a large DynamicModule).

share|improve this question
    
Actually to get rid of creation of buttons Manipulate is used. As sample add one more parameter(g) to it Manipulate[Plot[Sin[k*x + g], {x, 0, 10}], {k, 1, 10}, {g, 1, 10}] controls for k ,g are created automatically. –  Rorschach Jul 14 '13 at 12:17
    
If h is really huge I advise You to not put it all inside Dynamic. Add Dynamic only to k wherever it is (pay attention to Heads) or in case of Manipulate include only that Button and maybe append TrackedSymbols:>{ k, ...}. –  Kuba Jul 14 '13 at 14:08

2 Answers 2

This is just an addendum to jVincent's answer.

I do this sort of thing all the time when a full-bore Manipulate would be overkill. I recommend adding a Reset button and a dynamic label to make the dynamic behavior more Manipulate-like.

h := Plot[Sin[k x], {x, 0, N[4 Pi]},
  PlotLabel -> Sin[k x],
  ImageSize -> Medium];
btns = Row[{Button["k++", k++], Button["Reset", k = 1]}, Spacer[10]];
k = 1; Column[{Dynamic@h, btns}]

DynamicPlot

share|improve this answer

If I understand you correctly, you can simply display h in a Dynamic and then create your button to increase k, so first define your module:

k = 1;
h := Plot[Sin[k*x], {x, 0, 10}]

Then display the plot and button:

Button["Increase k", k = k + 1]
Dynamic[h]
share|improve this answer

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.