7
$\begingroup$

So as the title says I am trying to make a list where each element is determined by a users choice of an element in a PopupMenu.

My first attempt:

test = Table["A", {5}];
Table[PopupMenu[Dynamic[test[[n]]], {"A", "B", "C"}], {n, 5}]

Returned the following error

Part::pspec: Part specification n is neither an integer nor a list of integers.

For some reason the dynamic(?) would not allow me to refer to specific elements in the list. I then tried to circumvent this issue by introducing an extra variable temp:

Table[temp = n;PopupMenu[Dynamic[test[[temp]]], {"A", "B", "C"}], {n, 5}]

However, all this did was create 5 PopupMenus that all referred to the $5^{\text{th}}$ element of the list test. I tried to put a Setting[] around the Dynamic[], but since that removes that effect of Dynamic[] nothing happened at all.

Any suggestions would be greatly appreciated.

$\endgroup$

2 Answers 2

8
$\begingroup$

This seems to work :

test = Table["A", {5}];

PopupMenu[Dynamic[test[[#]]], {"A", "B", "C"}, "A"] & /@ Range[5] // Row

Dynamic @ test

(* {"C", "A", "B", "A", "C"} *)
$\endgroup$
1
  • $\begingroup$ Thanks! That was exactly what I was looking for. $\endgroup$
    – E.O.
    Aug 4, 2012 at 11:36
5
$\begingroup$

You can also use:

 test = Table["A", {5}];
 Table[With[{n = n}, PopupMenu[Dynamic[test[[n]]], {"A", "B", "C"}]], {n, 5}]

or

 Table[PopupMenu[Dynamic[test[[k]]], {"A", "B", "C"}] /. k -> n, {n, 5}]
$\endgroup$
2
  • $\begingroup$ Thanks for the answer as well! Just one question though; how come the With[{n=n},.....] works? $\endgroup$
    – E.O.
    Aug 4, 2012 at 12:33
  • 1
    $\begingroup$ @E.O., With[{n=n},..] is used to replace all instances of the local variable n with its corresponding value ("so that the values of the Table iterator variable are passed into the dynamic expression"). See How to Evaluate Expressions inside Dynamic or Manipulate for several examples on the reason why it is needed and works here. $\endgroup$
    – kglr
    Aug 4, 2012 at 13:57

Your Answer

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

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