1
$\begingroup$

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}]]
$\endgroup$

2 Answers 2

1
$\begingroup$

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}]
  ]
 ]
$\endgroup$
4
$\begingroup$

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

$\endgroup$

Your Answer

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

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