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.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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"} *)
share|improve this answer
    
Thanks! That was exactly what I was looking for. –  E.O. Aug 4 '12 at 11:36

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}]
share|improve this answer
    
Thanks for the answer as well! Just one question though; how come the With[{n=n},.....] works? –  E.O. Aug 4 '12 at 12:33
1  
@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. –  kguler Aug 4 '12 at 13:57

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.