1
$\begingroup$

I want to learn some of the basic rules for construction DynamicModule code. I have looked at Kuba's post and feel overwhelmed by it. My ultimate goal is to use Switch in a DynamicModule that will provide different GUI's, each of which would plot equations produced by different algorithms.

So I began my journey by trying to transform the following Manipulate code to DynamicModule code:

Manipulate[
Plot[Evaluate[x[t, u]], {t, 0, 2}, PlotRange -> {-1, 1}, 
Exclusions -> None],
{{u, 1, " "},  {1 -> A, 2 -> B, 3 -> C}},
TrackedSymbols :> {u, f},
Initialization :> (
x[t_, u_] := Switch[u,
  1, Sin[2 \[Pi] t],
  2, SquareWave[t],
  3, TriangleWave[t]];
)
]

The DynamicModule code that I came up with doesn't work. For starters, I dpn't know where to place the Initialization code. As you can see my my futile attempt, I lack understanding of the basic structure of DynamicModule.

DynamicModule[{},
Dynamic@Plot[x[t, u], {t, 0, 2}, PlotRange -> {-1, 1}, 
Exclusions -> None],
{{u, 1, " "}, {1 -> A, 2 -> B, 3 -> C}},
Initialization :> (
x[t_, u_] := Switch[u,
  1, Sin[2 \[Pi] t],
  2, SquareWave[t],
  3, TriangleWave[t]];
)
]

Some guidance is needed.

$\endgroup$
  • 1
    $\begingroup$ That topic is quite old and may be vague, I will have to rephrase all that in context of what I've learned about GUIs creation in MMA. But going back to the question, Initialization is in a good place but you have to take care about Controls spec. While Manipulate translates {u, 1, " "} to GUI and puts it next to the body, in DynamicModule you have to do this manually. Use Grid/Row/Column/Panel for positioning and Slider/PopupMenu and friends for those controllers. $\endgroup$ – Kuba Mar 8 '16 at 22:01
  • $\begingroup$ If you are not very familiar with Dynamic functionality, my advice is to start off with Manipulate. It's fine for simpler GUIs for personal use, for class demonstrations, and so forth. However, if you keep running up against the limitations of Manipulate (and not just the limits of your knowledge), or if you are creating a commercial or business product, in which reliability and robustness are important, then you will need the control over things that you get with DynamicModule. $\endgroup$ – Michael E2 Mar 8 '16 at 23:08
3
$\begingroup$

As indicated by Kuba you have to take charge of the controls when DyanmicModule is used.

Here is a simple example from the documentation on DyanmicModule.

DynamicModule[{x}, {Slider[Dynamic[x]], Dynamic[x]}]

Mathematica graphics

For your particular example below is a method that uses Column. Note that using Initialization will set the global value of the function x.

DynamicModule[
 {
  u
  },

 Column[{
   Dynamic@Plot[Evaluate[x[t, u]], {t, 0, 2},
     PlotRange -> {-1, 1},
     Exclusions -> None,
     ImageSize -> 400],

   SetterBar[Dynamic[u], {1 -> A, 2 -> B, 3 -> C}]
   }, Alignment -> Center
  ],

 Initialization :> (
   x[t_, u_] := Switch[u,
     1, Sin[2 \[Pi] t],
     2, SquareWave[t],
     3, TriangleWave[t]
     ]
   )

 ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Very helpful. Your example was much better than any of the others I've seen in the Wolfram documentation. $\endgroup$ – Stephen Mar 9 '16 at 3:28

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.