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.

Bear with me, I'm a noob; I simply want to create a radio button bar that selects whether I want to add or subtract two numbers:

{RadioButtonBar[Dynamic[stringvar], {"*" -> "Multiply Numbers", "+" -> "Add Numbers"}]}

And my equation might simply be:

2 + 2

And I just want to change the + to a * interactively. Not sure if this is the right way to do it. Thanks.

share|improve this question
3  
Create a Dynamic variable that is the Head of an expression, and can take the values Plus or Times –  Verbeia Apr 19 '13 at 14:02

2 Answers 2

up vote 5 down vote accepted

From the documentation:

Manipulate[operator[x, y], 
 {{x, 2}, InputField}, 
 {{y, 2}, InputField}, 
 {operator, {Plus, Times, Subtract, Divide}}, 
 SaveDefinitions -> True]

picture

share|improve this answer

Try

Column[
 {Row[{InputField[Dynamic[x]], InputField[Dynamic[y]]}],
  RadioButtonBar[
   Dynamic[op], {Times -> "Multiply Numbers", 
    Plus -> "Add Numbers"}],
  Dynamic@op[x, y]
  }
 ]

enter image description here

Notice how code and data are the same thing in Mathematica, so I can use the fact that the operators Plus and Times can be stored in variables to construct the interface.

You can also use arbitrary dynamic controls here

Column[
 {Column[{InputField[Dynamic[x]], InputField[Dynamic[y]]},
   Frame -> True,
   FrameStyle -> Directive[Orange, Thick, Dashed]],
  PopupMenu[Dynamic[op], {Plus, Times, Subtract, Divide}],
  Dynamic@op[x, y]
  }
 ]

enter image description here

share|improve this answer
    
you're quick today, I just copied mine :) –  cormullion Apr 19 '13 at 14:02
    
I just happened to come here when the question was asked :) –  acl Apr 19 '13 at 14:05

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.