Creating specific options in `Manipulate[]`

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


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)


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

Tags:

Manipulate