2
$\begingroup$

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]]]}]]
$\endgroup$
1

1 Answer 1

3
$\begingroup$
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

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – dabd
    May 2, 2013 at 18:34
  • $\begingroup$ Now I am struggling to add another feature: how to make the sliders always add to 1? $\endgroup$
    – dabd
    May 2, 2013 at 20:06
  • $\begingroup$ 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. $\endgroup$
    – dabd
    May 2, 2013 at 20:29
  • $\begingroup$ Never mind I was confused, normalization makes sense for this case as well. Thanks. $\endgroup$
    – dabd
    May 2, 2013 at 20:35
  • $\begingroup$ 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. $\endgroup$
    – dabd
    May 2, 2013 at 21:10

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.