So I can do something like this which I like:

Manipulate[i, {i, {1,2,3,4}}]

It lets me pick which specific values I want to allow to be chosen for my function. But that list appears to be very limiting.

Lets say I have a list and each element contains a list of two elements like so:

myList = {{1,2},{3,4}}

How can I use Manipulate with this list such that it would give me two options to choose from: {1,2} and {3,4}

Here is what I have tried:

Manipulate[i, {i, myList}]

But it seems to only get it right on initialization and then when you touch the slider it goes haywire and starts choosing things like 1 and 3 intsead of {1,2} and {3,4}

I want to be able to use Manipulate but only have it work on a set pair of numbers.

  • 1
    maybe Manipulate[i, {i, myList, SetterBar}] or Manipulate[i, {i, myList, RadioButtonBar}]? – kglr Jan 5 '15 at 6:17
  • 2
    Or as you can find it in the documentation to ControlType: Manipulate[i, {i, {{1, 2}, {3, 4}}, ControlType -> SetterBar}] – halirutan Jan 5 '15 at 6:22
up vote 2 down vote accepted

I suspect that you can obtain what you need by following way:

list = {{1, 2}, {3, 4}, {5, 6}};

Manipulate[list[[i]], {i, 1, Length@list, 1}]

This code always gives you the element (sublist) of initial list.

  • Just what I needed thanks a bunch! – user1886419 Jan 5 '15 at 18:07

The problem is caused by ambiguity in the control-inferencing logic used by Manipulate and Control in the absence of an explicit control type specification. A Manipulate value with a list of pairs is a valid specification for a Slider, SetterBar, PopupMenu or InputField. Manipulate arbitrarily chooses to use a slider.

Mathematica uses various heuristics to determine what type of control to use. These heuristics can be seen, for example, by inspecting the down-values of Manipulate`Dump`parameterToControls. In version 10.0.2, the rules that are applicable when the control specification has the exhibited form are as follows (although I might have missed some rules in my quick scan):

  • if the value is a list comprised of two-element sublists then use a Slider
  • if the value is {True,False}, {False,True}, {0,1} or {1,0} then use a Checkbox
  • if the value is a list has two to five values then use a SetterBar
  • if the value is any other kind of list then use a PopupMenu
  • if the value is a color then use a ColorSlider
  • if the value is Dynamic[...] then use a Manipulator
  • otherwise use an InputField

We can see that the first heuristic is being satisfied in our example, so a Slider is being used. For a Slider, the specification {{1,2},{3,4}} indicates two values: the value 1 with relative width 2 and the value 3 with the relative width 4. Since there are only two values in the list, the relatives widths are unobservable in the slider behaviour.

We can also see that there are overlaps between the heuristic rules when it comes to lists. The choice of control is somewhat arbitrary for the lists that fall into those overlaps. If we are not satisfied with the heuristic choice, our only recourse is to explicitly specify the control type ourselves, e.g.:

Manipulate[i, {i, {{1, 2}, {3, 4}}, SetterBar}]

or

Manipulate[i, {i, {{1, 2}, {3, 4}}, ControlType -> PopupMenu}]

If we actually want to use a slider, then it is a bit trickier as we must work around the {value, width} notation in Slider:

Manipulate[i, {{i, {1, 2}}, Slider[Dynamic[i], {{{{1, 2}, 1}, {{3, 4}, 1}}}] &}]
  • I am deleting my answer in favor of your more complete and correct one. Thanks for imparting knowledge. :-) – Mr.Wizard Jan 6 '15 at 4:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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