Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I seem to be getting some unintended results from a nested Manipulate that I have not been able to resolve. I boiled down the problem I'm having to a simplified version. I can not seem to get the evaluation order of Manipulate correct.

Manipulate[
 Manipulate[
  Print[{Parity, n}];
  {Parity, n},
  {n, Select[Range[1, 10, 1], Parity]}
 ],
 {Parity, {OddQ, EvenQ}}
]

Mathematica graphics

The problem isn't what it shows in the window, it is with what the Print statement outputs. For instance, if it is in the state EvenQ,2 and I click on OddQ, the Print output will read:

{OddQ,2}

Despite the fact that it shows {OddQ,1} in the window.

Why is this, and how can I fix it? In this boiled-down example it doesn't matter, but in my real life one, an error is issued every time I switch the outer manipulate.

share|improve this question
    
This is weird indeed. Change, for instance, the line {Parity, n} to a={Parity, b=n} and evaluate a Dynamic[{a,b}] in a new cell and see what happens then if you press the parity buttons. The value of a and b do not correspond to the displayed value of n in the Manipulate box even though they are the results of an assignment using the same n. –  Sjoerd C. de Vries Mar 21 '12 at 22:27
    
If I add the option TrackedSymbols :> {n} to the inner manipulate, the incorrect print output disappears and the window displays correct at all times. This solution prevents the error I was getting in the un-boiled-down problem. –  Chris Roth Mar 22 '12 at 16:11

2 Answers 2

up vote 3 down vote accepted

I can't help you with why your code doesn't work, but this seems to work. In this solution n is reset to either 1 or 2 when Parity is changed before Print is called.

Manipulate[Manipulate[
  If[Not[Parity[n]], n = Mod[n + 1, 2, 1]];
  Print[{Parity, n}];
  {Parity, n},
  {n, Select[Range[1, 10, 1], Parity]}], {Parity, {OddQ, EvenQ}}]
share|improve this answer

I don't think you should be nesting Manipulate objects. The fact that you are dealing with Dynamic objects means that things often don't work quite as they do otherwise.

See: Why won't this work? Dynamic in a Select

Here is an example of a scoping irregularity that is easily produced with Block and Manipulate:

Parity = "You shouldn't see this.";

Block[{Parity = EvenQ},
 Manipulate[
  {Parity, n},
  {n, Select[Range@10, Parity]}
 ]
]

Mathematica graphics

I recommend that you use Manipulate only in simple cases, turning to manual construction with DynamicModule, Control, etc. when you need something complicated.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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