# How to programmatically create a Manipulate in another Manipulate?

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?

-
Can you explain why the first variant where you entered {a, 1, db[[x, 2]]} literally won't work for your application? –  Szabolcs Apr 10 '12 at 14:57
The inner-manipulate can have a variable number of controls depending on the outer. –  ndroock1 Apr 10 '12 at 15:09
Colors, in fact. A geometry file is read from a database the number of faces is determined and then a color changing manipulate is ( supposed to be ) set up. –  ndroock1 Apr 10 '12 at 15:11
@ndroock1 I've updated my answer so that the sliders work properly, and have taken a stab at explaining what went wrong. –  Pillsy Apr 10 '12 at 18:15
Just in case you have not already seen: this question and several answers therein. –  kglr Apr 10 '12 at 19:01

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:

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

-
Did not work at first but it was because I had an x defined elsewhere in my session, looks good. Will try to complete it. –  ndroock1 Apr 10 '12 at 16:24
Piilsy - But x[i] is displayed instead of the value, have you noticed? –  ndroock1 Apr 10 '12 at 16:27
Nice job! , Pillsy. This is a flexible multi-level, multi-variable Manipulate setup. Thank you. –  ndroock1 Apr 10 '12 at 19:01