2
$\begingroup$

Here's a very simple MWE to show my problem :

Manipulate[
    Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
    {{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
    {{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01}
]

This code produces a simple graphics with two sliders : parameters $a$ and $b$.

I would like to make the second slider to become all grayed out (i.e. unavailable, with $b$ set to 0) if the user moves the first slider to $a \le 0$.

How to achieve this ? This is certainly very basic (?) but I'm yet unable to see how.

$\endgroup$
3
  • 3
    $\begingroup$ To grey out the slider, add Enabled -> Dynamic[a > 0] as an option to the b variable specification. $\endgroup$ – lowriniak Dec 20 '16 at 17:05
  • $\begingroup$ @lowriniak, wow ! It works perfectly. However, the $b$ parameter should turn to 0. How to add this constraint ? $\endgroup$ – Cham Dec 20 '16 at 17:14
  • 1
    $\begingroup$ Aah ! This actually solves totally my problem ! I suggest that you write a small answer with the code, so I could mark it (and you get more reputation points) ! $\endgroup$ – Cham Dec 20 '16 at 17:19
4
$\begingroup$

So to expand a little on my comment, to make a slider be greyed out you can use Enabled:

Manipulate[
    Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
    {{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
    {{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> False}
]

Enabled->False

From the tutorial AdvancedManipulateFunctionality, see the section Interdependent Controls - this shows some examples of controls depending on each other. This means we can use a in the definition of b:

Note: in my comment I added a Dynamic which I don't think was strictly necessary

Manipulate[
    Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
    {{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
    {{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> (a > 0)}
]

Above0 Below0

If you wanted more dependence on a, it is fairly easy to just use If statements in the body of your code:

Manipulate[
    If[a < 0, b = 0];
    Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
    {{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
    {{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> (a > 0)}
]
$\endgroup$
1
  • $\begingroup$ Apparently, the Dynamic command need to be there for my code to properly work. Without it, the slider isn't grayed out. Strange, since your code DO work ! $\endgroup$ – Cham Dec 21 '16 at 13:48

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.