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 would like to generate dynamic Manipulators for each of the selected buttons in a ToggleBar. How can I keep the state of each Manipulator in a variable? I would like to later access the values of the Manipulators for each of the selected buttons.

Thanks.

DynamicModule[{stakes = {}}, 
 Column[{TogglerBar[Dynamic[stakes], {1.5, 3.5, 7, 15, 30, 60, 100, 
                                      200, 300, 500, 1000}], 
         Dynamic[Grid[({#, Manipulator[Appearance -> Labeled]}) & /@ Sort[stakes]]]}]]
share|improve this question
    
In this answer I did something very similar mathematica.stackexchange.com/a/14149/193 –  belisarius May 2 '13 at 16:50

1 Answer 1

up vote 3 down vote accepted
m = {1.5, 3.5, 7, 15, 30, 60, 100, 200, 300, 500, 1000};
(x[#] = 0) & /@ m;
DynamicModule[{stakes = {}}, Column[{TogglerBar[Dynamic[stakes], m],
   Dynamic[
    Column@{Grid[({#,
           Manipulator[Dynamic[x[#]], Appearance -> Labeled],
           Dynamic[x[#]]}) & /@ Sort[stakes]],
      Dynamic@Total[x /@ m]}]}]]

enter image description here

Edit

Normalized readings (as requested in your comments):

m = {1.5, 3.5, 7, 15, 30, 60, 100, 200, 300, 500, 1000};
(x[#] = 0) & /@ m;
DynamicModule[{stakes = {}}, 
 Column[{TogglerBar[Dynamic[stakes], m], 
   Dynamic[Column@{Grid[({#, 
           Manipulator[Dynamic[x[#]], 
            Appearance -> Labeled], (Dynamic[
             Quiet@Check[x[#]/Total[x /@ stakes], 0]])}) & /@ 
        Sort[stakes]]}]}]]

enter image description here

share|improve this answer
    
Thanks. I figured a similar solution but yours is much better. I was struggling to find a way to use Total over the array x since it is not a list and you can't use Total directly. So an array x is a function of its keys in Mathematica? At least that is how I interpret x /@ m. –  dabd May 2 '13 at 18:34
    
Now I am struggling to add another feature: how to make the sliders always add to 1? –  dabd May 2 '13 at 20:06
    
I think for my application it makes sense. Each slider represents the stakes of a game. If I am going to play 100 games then choosing 1.5 at 0.6 and 3.5 at 0.4 means I am playing 60 of the games at 1.5 stake and the remaining 40 at 3.5 stake. It does not make sense to say I am playing 100% of the games at 1.5 and 100% of the games at 3.5, for example. –  dabd May 2 '13 at 20:29
    
Never mind I was confused, normalization makes sense for this case as well. Thanks. –  dabd May 2 '13 at 20:35
    
I didn't know about the Quiet Check trick. However there is still one problem. If I click 15 and move the slider to 1, click 30 and move the slider to the somewhere below 1, then if you toggle 30, removing it the 15 slider will keep at a smaller value than 1. –  dabd May 2 '13 at 21:10

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.