7
$\begingroup$

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?

$\endgroup$
7
  • $\begingroup$ Can you explain why the first variant where you entered {a, 1, db[[x, 2]]} literally won't work for your application? $\endgroup$
    – Szabolcs
    Commented Apr 10, 2012 at 14:57
  • $\begingroup$ The inner-manipulate can have a variable number of controls depending on the outer. $\endgroup$ Commented Apr 10, 2012 at 15:09
  • $\begingroup$ 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. $\endgroup$ Commented Apr 10, 2012 at 15:11
  • $\begingroup$ @ndroock1 I've updated my answer so that the sliders work properly, and have taken a stab at explaining what went wrong. $\endgroup$
    – Pillsy
    Commented Apr 10, 2012 at 18:15
  • $\begingroup$ Just in case you have not already seen: this question and several answers therein. $\endgroup$
    – kglr
    Commented Apr 10, 2012 at 19:01

1 Answer 1

11
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Apr 10, 2012 at 16:24
  • $\begingroup$ Piilsy - But x[i] is displayed instead of the value, have you noticed? $\endgroup$ Commented Apr 10, 2012 at 16:27
  • $\begingroup$ Nice job! , Pillsy. This is a flexible multi-level, multi-variable Manipulate setup. Thank you. $\endgroup$ Commented Apr 10, 2012 at 19:01

Your Answer

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

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