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.

Right now I have a blank list that I am putting in Manipulate[]. I want to be able to choose whether 1,2, or 3 are in this list by setting z1,z2, z3 as True or False:

 z1 = True;
 z2 = True;
 z3 = False;
 list = {};
 For[i = 1, i <= 3, i++, 
  If[ToExpression["z" <> ToString@i], list = Insert[list, i, 1]]]
 list

The problem comes when I put this code into Manipulate[], where I have a button called "Update list" that essentially does the process above:

 Manipulate[{list, {z1, z2, z3}}, 
  Row[{Control[{z1, {True, False}}], Control[{z2, {True, False}}], 
    Control[{z3, {True, False}}]}], 
  Button["Update list",list = {}; 
  For[i = 1, i <= 3, i++, 
 If[ToExpression["z" <> ToString@i], 
  list = Insert[list, i, 1]]]]]

But as you shall see, the button does nothing. I want:

if box "z1" is checked, 1 is added to the list. if box "z2" is checked, 2 is added to the list. ... if box "zi" is checked, i is added to the list.

How do you do this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Your approach is so convoluted, that I find to hard to see what you are really trying to accomplish, but the following simple Manipulate accomplishes what I interpret your example code is trying to do.

Manipulate[{listy, {z1, z2, z3}},
  Row[{Control[{z1, {1, 0}}],
    Control[{z2, {1, 0}}],
    Control[{z3, {1, 0}}]}], 
  Button["Apply Filters", listy = {z1, z2, z3}],
  {{listy, {z1, z2, z3}}, None}]

manip.png

Edit

Now that I understand better what you want, cosider

Manipulate[{listy, {z1, z2, z3}},
  Row[{
    Control[{z1, {1, 0}}],
    Control[{z2, {1, 0}}],
    Control[{z3, {1, 0}}]}], 
  Button["Apply Filters", listy = Flatten @ Position[{z1, z2, z3}, 1]],
  {{listy, {1, 2, 3}}, None}]

manip2.png

Perhaps you would also be interested in the more difficult case where the number of checkboxes is a parameter.

With[{n = 4},
  Manipulate[{listy, vars},
    Dynamic@Row[checkBoxes, " "],
    Button["Apply Filters", listy = Flatten@Position[vars, 1]],
    {{listy, {}}, None},
    {checkBoxes, None},
    {names, None},
    {vars, None},
    Initialization :> (
     Clear["z*"];
     names = Table["z" <> ToString@i, {i, n}];
     vars = ToExpression /@ names;
     checkBoxes = 
       Flatten@MapThread[{#1, Control[{#2, {1, 0}}]} &, {names,  vars}])]]

manip3.png

Edit 2

After some thinking, I found a way to set the number of checkboxes interactively. To do this I had to more or less turn the problem inside-out. By this I mean that, in this version, the list of checkbox values is derived from listy, rather than the other way around. I was also forced to use one of Mathematica's more obscure dynamic features to force reinitialization when the user changes the number of checkboxes. Nevertheless, the final result is a fairly compact and IMO elegant expression.

Manipulate[
  {Sort@listy, ReplacePart[ConstantArray[0, n], List /@ listy -> 1]},
  {{n, 3, "How many checkboxes?"}, 2, 6, 1, Appearance -> "Labeled"},
  Dynamic@Row[Refresh[listy = {}, TrackedSymbols -> {n}], ""],
  {{listy, {}, ""}, Table[i -> "z" <> ToString@i, {i, n}], ControlType -> CheckboxBar}]

manip4.png

share|improve this answer
    
Hey, m_goldberg, thanks for your response. But that is not quite what I was trying to accomplish. I don't want a list of 1's. For the image you attached to your answer, I would want Apply Filters to set listy={2,3}. If all boxes are checked, I would want Apply Filters to set listy={1,2,3}. If none are checked, listy={}, and so on... Thanks a lot though for submitting a response! :) –  David Aug 15 '13 at 18:22
    
So box z1 determines whether 1 is in the list, box z2 determines whether 2 is in the list, and so on. –  David Aug 15 '13 at 18:24
    
I just edited the code to make it clearer what I am doing - sorry that my question was so convoluted –  David Aug 15 '13 at 18:32
    
That works! Thanks! –  David Aug 15 '13 at 20:53

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.