1
$\begingroup$

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 ?

$\endgroup$
5
  • 1
    $\begingroup$ add If[f == Null, f = 0]; or something like that $\endgroup$
    – garej
    Commented Feb 6, 2016 at 19:15
  • 1
    $\begingroup$ to hide "InputField": AppearanceElements -> {"StepLeftButton", "PlayPauseButton", "StepRightButton", "FasterSlowerButtons", "DirectionButton"} $\endgroup$
    – garej
    Commented Feb 6, 2016 at 19:25
  • 1
    $\begingroup$ @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. $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 19:32
  • $\begingroup$ And this is very usefull to return to the preset values ! $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 19:38
  • $\begingroup$ 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 ? $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 19:41

2 Answers 2

1
$\begingroup$

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
 ]
$\endgroup$
2
  • $\begingroup$ 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 ! ;-) $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 21:19
  • $\begingroup$ @Cham, glad to help. $\endgroup$
    – garej
    Commented Feb 6, 2016 at 21:24
1
$\begingroup$

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]

demo

$\endgroup$
10
  • $\begingroup$ 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 ? $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 19:54
  • 2
    $\begingroup$ @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) $\endgroup$
    – garej
    Commented Feb 6, 2016 at 20:09
  • $\begingroup$ @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. $\endgroup$
    – m_goldberg
    Commented Feb 6, 2016 at 20:18
  • $\begingroup$ @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 ? $\endgroup$
    – Cham
    Commented Feb 6, 2016 at 20:24
  • 1
    $\begingroup$ @Cham, note that Not@NumericQ covers Null case. $\endgroup$
    – garej
    Commented Feb 6, 2016 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.