Consider the following ( working ) snippet:

     {vote, {"Yes", "No"}},
     Initialization :> {votes = 0}
    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.

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

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.

    {{vote, voteList[[k]]}, {True, False}, 
       Dynamic[vote, (vote = #; voteList[[k]] = #; 
          votes = Total@Boole@voteList) &], {True -> "Yes", 
        False -> "No"}] &}
   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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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