2
$\begingroup$

I'm looking for a simple example about how to add a simple On-Off switch in a manipulate function.

More specifically, I defined a curve with two parameters. One is a continuous parameter that is changed using the manipulate slider. The other one is a discrete parameter that takes two values only : 0 or 1. I need to put that parameter as a check box in the manipulate output, above the continuous slider. The check box should have a small line of text, which changes if the box is in its 0 state or its 1 state.

This is certainly very basic, but I can't find any clear example anywhere.

How can I implement what I described?

$\endgroup$
1
  • $\begingroup$ it's not a checkbox but maybe works? Manipulate[Plot[Sin[a x], {x, 0, 10}], {a, 0, 1, 1}] $\endgroup$ Commented Feb 1, 2016 at 2:14

2 Answers 2

2
$\begingroup$

I just want to point out the Edmund's fancy version of Manipulate is unnecessarily complicated. The same effects can be gotten with much simpler code.

Manipulate[
  Plot[Sin[a + b x], {x, 0, 10}],
  Row[{
    Control[{{a, 0, ""}, {0, 1}}],
    "  ",
    Dynamic @ Switch[a, 0, "Zero", 1, "One"]}],
  {{b, 1, ""}, 1, 5, Appearance -> "Labeled"}]

demo

Manipulate allows a great deal of customization of controls, so it is seldom really necessary to use the kind of lower-level dynamic code that Edmund invoked (although such certainly works).

Also note that to insert a control specification such as {{a, 0, ""}, {0, 1}} into a formatting construct such as Row, one need only wrap it with Control.

Update

In a comment below, the OP asks: How do you place the button to the left, and the slider to the right?

Manipulate[
 Plot[Sin[a + b x], {x, 0, 10}],
   Row[{
     Control[{{a, 0, ""}, {0, 1}}],
     "  ",
     Dynamic @ Switch[a, 0, "Zero", 1, "One"],
     Spacer[80],
     Control[{{b, 1, ""}, 1, 5, Appearance -> "Labeled"}]}]]

variant

The space between the two controls can be adjusted by changing the argument given to Spacer. For demonstration purposes, I have chosen a value that moves the slider to the far right.

$\endgroup$
4
  • $\begingroup$ How do you place the button to the left, and the slider to the right ? $\endgroup$
    – Cham
    Commented Feb 1, 2016 at 14:22
  • $\begingroup$ So there isn't a command to push the slider to the right ? We need to adjust it by eye, is that right ? $\endgroup$
    – Cham
    Commented Feb 1, 2016 at 18:54
  • $\begingroup$ @Cham. I know of no simple command that will make the controls in a Row behave as there were a spring spacer between them. pushing them to space out over the full width of the panel they located in. $\endgroup$
    – m_goldberg
    Commented Feb 2, 2016 at 2:24
  • $\begingroup$ Ok, thanks a lot. So I'll use a spacing by eye-hand. It's all working great anyway. $\endgroup$
    – Cham
    Commented Feb 2, 2016 at 15:48
5
$\begingroup$

A simple way is to define your Manipulate variable parameters so that they are recognised as the control type you want. Look in the first part of the Details and Options section of Manipulate documentation.

Manipulate[
 Plot[Sin[a + b x], {x, 0, 10}], {{a, 0}, {0, 1}}, {b, 1, 5}]

enter image description here

You can get fancier by specifying the controls and layout directly.

Manipulate[Plot[Sin[a + b x], {x, 0, 10}],
 Column@{
   Row[{Checkbox[Dynamic@a, {0, 1}], Spacer[5], 
     Dynamic@Switch[a, 0, "Zero", 1, "One"]}],
   Slider[Dynamic@b, {1, 5}, Appearance -> "Labeled"]}
 ]

enter image description here

Hope this helps.

$\endgroup$
3
  • $\begingroup$ Thanks a lot. It's working. I think this is it. I'll wait a bit in case someone else find a simpler method, before checking this as "THE" answer. $\endgroup$
    – Cham
    Commented Feb 1, 2016 at 2:34
  • $\begingroup$ Why the Spacer[5] command ? Removing it doesn't change anything, apparently. $\endgroup$
    – Cham
    Commented Feb 1, 2016 at 2:36
  • $\begingroup$ Duh ! I see the spacing now ! $\endgroup$
    – Cham
    Commented Feb 1, 2016 at 2:38

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.