3
$\begingroup$

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.

$\endgroup$
  • 3
    $\begingroup$ Create a Dynamic variable that is the Head of an expression, and can take the values Plus or Times $\endgroup$ – Verbeia Apr 19 '13 at 14:02
5
$\begingroup$

From the documentation:

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

picture

| improve this answer | |
$\endgroup$
4
$\begingroup$

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

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

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.