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.

Consider the following ( working ) snippet:

  Manipulate[
   Column[{
    Manipulate[
     vote,
     {vote, {"Yes", "No"}},
     Initialization :> {votes = 0}
    ],
    k,
    Button["TEST", Print[votes]]
   }],
   {k, 1, 5, .5}
  ]

My question concerns programming style and Outer/Inner Manipulate structures. What is the best way to handle code that is executed conditionally based on values in the inner Manipulate, but needs to be processed further in the outer Manipulate? I.e. checking thresholds, maintaining sub-totals, balancing, etc.

To make my question 'answerable' I have translated it in terms of a simplified example: how / where would you maintain a Yes vote counter that is used in the outer manipulate?

UPDATE: The requested pattern would also apply to synchronizing views in data on Tabs on a TabView.

share|improve this question
    
I edited the title to better reflect the actual problem here, please feel free to revert/change if you think it is not correct. –  István Zachar Apr 30 '12 at 12:51
add comment

1 Answer 1

up vote 2 down vote accepted

The outer Manipulate defines a list (voteList) for tracking all votes (thus I used integer values for k), and the inner Manipulate is using the actual value of the kth vote for the definition of vote. If the inner Yes/No setter is changed, it changes vote, voteList and the number of votes as well, due to the pure controlfunction SetterBar. I also moved the initialization code to the outer Manipulate, as votes should be local to the whole thing, not just the inner Manipulate.

Manipulate[Column[{
   Manipulate[
    vote,
    {{vote, voteList[[k]]}, {True, False}, 
     SetterBar[
       Dynamic[vote, (vote = #; voteList[[k]] = #; 
          votes = Total@Boole@voteList) &], {True -> "Yes", 
        False -> "No"}] &}
    ],
   (*voteList,*)
   Button["Votes: " <> ToString@votes, Print[votes]]
   }],
 {k, 1, 5, 1, Appearance -> "Labeled"},
 {voteList, ControlType -> None},
 {votes, ControlType -> None},
 Initialization :> {votes = 0, voteList = Table[False, {5}]}]

Mathematica graphics

Of course in this example of yours there is no need to wrap the internal vote into Manipulate, but I guess that this was just a toy example of a more complex problem. I'm not sure my solution is eaxactly the thing you are looking for, but if you provide some feedback, I can modify the code to better fit your requirements.

share|improve this answer
    
This is just fine, Istvan. I have learned so much about Manipulate on this site. Thank you. –  ndroock1 Apr 30 '12 at 10:15
    
You're welcome. You might want to read on how to link dynamic controllers e.g. here: Manipulate with a variable number of sliders. –  István Zachar Apr 30 '12 at 12:49
add comment

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.