6

Background: the following Manipulate works as expected

 db := {{1, 10, 11}, {2, 20, 21}, {3, 5, 11}, {4, 15, 11}, {5, 8, 21}};
 Manipulate[
   Manipulate[ {a, 1, db[[x, 2]]}, {a, 1, db[[x, 2]]}],
 {{x, 1}, 1, 5, 1}]

Now I want to make the inner-Manipulate more flexible... I tried this:

 Manipulate[
   Manipulate[s, {a, 1, db[[x, 2]]}] /. s -> {a, 1, db[[x, 2]]},
 {{x, 1}, 1, 5, 1}]

Where s is supposed to be entirely variable based on some value in the outer-Manipulate, but it fails.

Question: I want to programmatically build a(n inner-)Manipulate from values selected in an (outer-)Manipulate. Is this possible, if so how?

UPDATE: I modified Piilsy's answer so that it works as follows:

 Manipulate[
  Manipulate[
    Column[Table[{##}[[i, 1, 1]], {i, 1, Length[{##}]}]], ##] 
    & @@Table[{{x[i], 0.5}, 0, 1}, {i, 1, n}], 
 {n, Range[6]}]

Question:Can this code be enhanced, i.e. made clearer?

nilo de roock
  • 9,657
  • 3
  • 35
  • 77

1 Answers1

11

EDIT to make the sliders work, in response to comments:

Here's one possible solution, wrapping your inner Manipulate in With statement that defines both the thing that you want to manipulate, and the Sequence of controls, like so:

Manipulate[
 With[{
   value    = Column[Table[x[i], {i, 1, n}]],
   controls = Sequence @@ Table[{{x[i], 0.5}, 0, 1}, {i, 1, n}]},
  Manipulate[
   value,
   controls]],
 {n, Range[6]}]

Both value and controls need to be defined in the same place because Manipulate, like some other constructs (including Module, Function and RuleDelayed) does some sort of symbol renaming for expressions inside of it, and you need the x in value to be the same x in controls.

This gives you a setup that looks like this:

nested Manipulates

If you try it out in a notebook, you will have n sliders in the inner Manipulate.

Pillsy
  • 18,498
  • 2
  • 46
  • 92