# Default values in Dynamic Switch in Manipulate

According to the following snippet:

Manipulate[{slide, cases},
{{cases, "a"}, {"a", "b"}},
Dynamic@Switch[cases,
"a", Control[{{slide, 0.5, "Slider a"}, 0, 1}],
"b", Control[{{slide, 0.1, "Slider b"}, 0, 1}]
]]


The value of slide is initialized to 0.1, and not 0.5 when, by default, the "a" case is selected, how to correct this behavior? In fact, even when "switching" between "a" and "b", it always take the default value of 0.1.

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 5 '16 at 17:05
• Do you expect the value of slide to be reset whenever cases is changed? That is not how the initialization value works. Initialization happens when the Manipulate first starts. – Michael E2 Nov 5 '16 at 23:48

You may use TrackingFunction and Dynamic.

Manipulate[
{slide, cases},
{{cases, "a"}, {"a", "b"},
TrackingFunction -> {None, Automatic, Switch[#, "a", slide = .5, "b", slide = .1] &}},
{{slide, 0.5, Dynamic@StringJoin["Slider ", cases]}, 0, 1}
]


The TrackingFunction on cases resets the value of slide when cases is updated. The Dynamic on the slide label means that it will always show the current value of cases.

Hope this helps.

• Perfect, this is what I was looking for, I think nesting Manipulates is inelegant, and perhaps also difficult to read. – Jerome Ibanes Nov 6 '16 at 2:34

One way to accomplish having the slider be dependent upon the position of the setter bar cases is to use nested Manipulates.

Manipulate[
With[
{
controls = Switch[
cases,
"a",
Control[{{slide, 0.5, "Slider a"}, 0, 1}],
"b",
Control[{{slide, 0.1, "Slider b"}, 0, 1}]
]
},

Manipulate[
{slide, cases},
controls]
],

{{cases, "a"}, {"a", "b"}}
]