Interactively Manipulate Code

3

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.

user7014

Posted 2013-04-19T13:58:45.463

Reputation: 33

3Create a Dynamic variable that is the Head of an expression, and can take the values Plus or Times – Verbeia – 2013-04-19T14:02:29.773

Answers

5

From the documentation:

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

picture

cormullion

Posted 2013-04-19T13:58:45.463

Reputation: 23 565

4

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

acl

Posted 2013-04-19T13:58:45.463

Reputation: 19 146

you're quick today, I just copied mine :) – cormullion – 2013-04-19T14:02:31.380

I just happened to come here when the question was asked :) – acl – 2013-04-19T14:05:03.603