5
$\begingroup$

Suppose I want to manipulate the plot of $A\sin(\omega t)$ with specific combinations of 3-parameter values, say $A$, $\omega$ and PlotStyle[].

Manipulate[
 Plot[A Sin[\[Omega] t], {t, 0, 2 \[Pi]}, 
  PlotStyle -> pStyle], {{A, \[Omega], 
   pStyle}, {{1, 10, Black}, {2, 15, Red}, {3, 5, Blue}}}] 

The objective is to keep only 3 choices of options (either 3 buttons or a drop down list with these specific combinations) open to the user instead of the regular 27 choices.

$\endgroup$
5
$\begingroup$

The problem is that Manipulate can't handle multiple variables for a single control, as you've probably noticed. One simple approach is to introduce a helper variable (setting in the code below) that can be set to the triples of values, which are then distributed onto the individual variables:

Manipulate[
 {A, ω, pStyle} = setting;
 Plot[A Sin[ω t], {t, 0, 2 π}, PlotStyle -> pStyle],
 {setting, {{1, 10, Black}, {2, 15, Red}, {3, 5, Blue}}}
 ]

enter image description here

Alternatively, you can create the SetterBar manually (similar to @SjoerdSmit's answer), since that supports "composite" variable specifications:

Manipulate[
 Plot[A Sin[ω t], {t, 0, 2 π}, PlotStyle -> pStyle],
 {
  setting, 
  SetterBar[
    Dynamic[{A, ω, pStyle}], {{1, 10, Black}, {2, 15, Red}, {3,
       5, Blue}}] &
  }
 ]

Note that I'm cheating a bit by giving the variable specification {setting, func&} to Manipulate, where func simply returns the SetterBar control. This is an easy way to specify a fully custom control with a label, but it might be considered a bit ugly (since setting is not actually used)

|improve this answer|||||
$\endgroup$
7
$\begingroup$
Manipulate[Plot[# Sin[#2 t], {t, 0, 2 π}, PlotStyle -> #3] & @@ setter,
 {{setter, {1, 10, Black}, Row[{"{A", "ω", "pStyle}"}, ","]},
  {{1, 10, Black}, {2, 15, Red}, {3, 5, Blue}}}]

enter image description here

|improve this answer|||||
$\endgroup$
3
$\begingroup$

What you were trying to do is actually possible if you define your own PopupMenu control:

Manipulate[
 Plot[A Sin[\[Omega] t], {t, 0, 2 \[Pi]}, PlotStyle -> pStyle],
 Grid[
  {{"A",
    PopupMenu[
      Dynamic[{A, \[Omega], pStyle}],
      # -> First[#] & /@ {{1, 10, Black}, {2, 15, Red}, {3, 5, Blue}}
    ]
    }}
 ]
]

In this example I decided to label the items in the menu with the values for A, but you can define other labels if you want. Note, however, that this doesn't localize the control variables. If you want that, use a DynamicModule around the Manipulate (which you can also use to initialize the values for the variables).

|improve this answer|||||
$\endgroup$

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.