# Manipulate slider freeze, and empty value in box

Here's a MWE code to show two problems I'm experiencing with the sliders and their value box, under Manipulate :

Manipulate[A = Min[A, Which[f < 0, 0.5, f >= 0, 1]];
Plot[A Sin[2 Pi f t/12], {t, 0, 12},
PlotRange -> {{0, 12}, {-1, 1}},
AspectRatio -> 0.5,
Frame -> True,
Axes -> True,
ImageSize -> 800],

Row[{
Control[{{f, 1, "frequency"}, -10, 10, 0.001, Appearance -> {"Labeled", "Closed"}}],
Spacer[125],
Control[{{A, 0.1, "Amplitude"}, 0, Dynamic[If[f < 0, 0.5, 1]], 0.001, Appearance -> {"Labeled", "Closed"}}]
}],
ControlPlacement -> Bottom]


Now, if you remove the value in the first slider box, then everything goes wrong. How to prevent this to happen ?

Also, from time to time, after some box value manipulation, I may get a slider freeze : unable to slide it in any way, except by recompiling the code. Why the slider freeze ? Is there a way to prevent that to happen ?

And how can I prevent the user to enter any out of range value in the slider's box ?

• add If[f == Null, f = 0]; or something like that Feb 6 '16 at 19:15
• to hide "InputField": AppearanceElements -> {"StepLeftButton", "PlayPauseButton", "StepRightButton", "FasterSlowerButtons", "DirectionButton"} Feb 6 '16 at 19:25
• @garej, the first suggestion is perfect ! Thanks a lot ! Now about the second, I want the parameters fields to be accesible, but if the user enters -15, say, it should return to some basic value. I guess the first suggestion is showing a way.
– Cham
Feb 6 '16 at 19:32
• And this is very usefull to return to the preset values !
– Cham
Feb 6 '16 at 19:38
• I think this should be set by default. Non-numeric symbols into the slider box ? Why someone would allow that ? Why it isn't numeric only by default ?
– Cham
Feb 6 '16 at 19:41

## 2 Answers

If there is a need to preserve initial structure of the code, some condition may be an option:

Manipulate[

dom = {-10, 10};
If[Not[NumericQ[f]] || Not[Between[f, dom]], f = 0];

Plot[a Sin[2 Pi f t/12], {t, 0, 12}, PlotRange -> {{0, 12}, {-1, 1}},
AspectRatio -> 0.5, Frame -> True, Axes -> True, ImageSize -> 800],

Row[{
Control@{{f, 1, "frequency"}, Sequence @@ dom, 0.001, Appearance -> {"Labeled"}},
Control@{{a, 0.1, "Amplitude"}, 0, If[f < 0, 0.5, 1], 0.001, Appearance -> {"Labeled"}}
}]
,
ControlPlacement -> Bottom
]

• Your remark above about the Null case included in the non-numeric case even helped me to solve a small spacement problem I had in LaTeX, since my Mathematica code was also presented into a PDF document made with LaTeX (and the Mathematica Listing package). This is AWESOME ! ;-)
– Cham
Feb 6 '16 at 21:19
• @Cham, glad to help. Feb 6 '16 at 21:24

You can avoid trouble by choosing reasonable values for the range and increment of your controls. The following choices work well.

Manipulate[
Plot[a Sin[2 Pi f t/12], {t, 0, 12},
PlotRange -> {{0, 12}, {-1, 1}},
AspectRatio -> 0.5,
Frame -> True,
ImageSize -> 700],
Row[{
Control[{{f, 1, "frequency"}, 0, 10, 0.01, Appearance -> "Labeled"}],
Spacer[135],
Control[{{a, .5, "amplitude"}, 0, 1, 0.01, Appearance -> "Labeled"}]}],
ControlPlacement -> Bottom]


• Why is the code in my question is giving troubles ? If I enter a letter, it fails, while your code don't fail with a non-numeric or if I empty the value box. What is the difference ?
– Cham
Feb 6 '16 at 19:54
• @Cham, because your code uses conditions, that, whenever variables get improper value, fails. This code doesn't use conditions and just ignore invalid values (but if you print letters they will appear in label and there will be no graph on the plot) Feb 6 '16 at 20:09
• @Cham. When I specified the ranges and increments of the controls, I was careful to assign reasonable values -- ones that the dynamic evaluator can handle. Negative frequencies cause trouble. I eliminated them. Increments of 0.001 over a range of -10 to 10 are too fine and make for updating problems. I used coarser ones. Try to work with the dynamic interface engine, not against it, and it will work for you. If you over-stress it, it will misbehave. Feb 6 '16 at 20:18
• @m_goldberg, thanks for the comment. But your way works well only for simple systems, like that oscillation (which is very basic). In my main code, I need to impose several conditions on the sliders. Do you agree that garej's suggestion is the right way to go, when conditions are presents ?
– Cham
Feb 6 '16 at 20:24
• @Cham, note that Not@NumericQ covers Null case. Feb 6 '16 at 21:04