2

I would like to create Manipulate which look like this

n = 4;
Manipulate[{a, b, c}, {a, Range[n]}, {b, Range[n]}, {c, Range[n]}]

but wherein list of controls defined dynamically. I tried this code, which does not work

vars = {a, b, c};
Manipulate[vars, Table[{vars[[i]], Range[n]}, {i, 1, vars // Length}]]
glS
  • 7,623
  • 1
  • 21
  • 61

2 Answers2

5

Generalizing to a variable number of controls and variable range of controls.

maxNbrControls = 10;
maxRngControls = 10;

Manipulate[
 Manipulate[
  Evaluate[
   a /@ Range[nbrControls]],
  Evaluate[
   Sequence @@ ({{a[#], 1, Subscript[a, #]},
        Range[rngControls],
        ControlType -> SetterBar} & /@
      Range[nbrControls])]],
 Row[{
   Control[{{nbrControls, 3, "Number of\nControls"},
     Range[maxNbrControls]}],
   Spacer[25],
   Control[{{rngControls, 4, "Range of\nControls"},
     Range[maxRngControls]}]}]]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
1

You need to tell Manipulate to Evaluate the expression generating the controls before the rest. Also you have to convert the single List generated by Table into a sequence of lists, each one specifying a single controller. For example with:

vars = {a, b, c};
n = 4;
Manipulate[
 Evaluate@vars,
 Evaluate[
  Sequence @@ Table[{vars[[i]], Range[n]}, {i, 1, vars // Length}]
  ]
 ]
glS
  • 7,623
  • 1
  • 21
  • 61