1
$\begingroup$

I tried the following code and others to change the type of operation using PopupMenu:

ClearAll[plus, minus, index, hesap];
plus[x_, y_] := x + y;
minus[x_, y_] := x - y;
index = {plus, minus};

Manipulate[
   hesap[x, y],
   {{hesap, 1, "operation type"},  
   Thread[Range[Length[index]]->index], ControlType -> PopupMenu},  
   {x, 1, 10},
   {y, 1, 10}
 ]

This code does not work in the way I expect. What is missing in the code?

$\endgroup$
3
  • 1
    $\begingroup$ Change index to index = {Plus, Subtract}; and change Manipulate[hesap[x, y], ... to Manipulate[index[[hesap]][x, y], ... $\endgroup$
    – flinty
    Aug 4, 2020 at 15:22
  • $\begingroup$ @flinty: I defined the {plus and minus} functions just to give an example. My original functions have different names and I like to recall them into Manipulate to do calculations. If I use {Plus, Subtract}, in effect I use Mathematica functions which I do not want to. $\endgroup$ Aug 4, 2020 at 15:28
  • $\begingroup$ @flinty: I got your answer. You are right that I missed the linkage to pick the right index element. I used my original functions with correct linkage. Thanks. $\endgroup$ Aug 4, 2020 at 15:37

1 Answer 1

2
$\begingroup$

Use an association to avoid indexing e.g.

ops = AssociationThread[{"Add", "Subtract"}, {plus, minus}];

Manipulate[ops[op][x, y],
 {{op, "Add", "operation type"}, Keys@ops, ControlType -> PopupMenu},
 {x, 1, 10}, {y, 1, 10}]
$\endgroup$
2
  • $\begingroup$ Please see my comment above. $\endgroup$ Aug 4, 2020 at 15:31
  • 1
    $\begingroup$ You can use whatever names you want, the names and the actual function names are independent and stored in the association. $\endgroup$ Aug 4, 2020 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.