# Generating a Variable Number of Dynamic Variables

I know questions that get at this very issue have been posed in other ways here, but as a relative beginner, I've had trouble understanding the answers.

For my application, I am developing a simple user interface that takes a dynamic variable through InputField[] which then specifies how many dynamic variables are to be generated in a list.

Here is a simple example of the error I encounter:

DynamicModule[{x = 5},

n = Dynamic@Table[Symbol["p" <> ToString@i], {i, 1, x}];

InputField[Dynamic@x]

n[[1]] ]


Above, x is a dynamic variable that specifies how many dynamic variables (of the name p1, p2, p3, etc.) are to be made in an automatically generated list. I then attempt to show the first dynamic variable in that list, and I get some strange output.

When x = 3, Mathematica makes a list of p1, p2, and p3 with their own InputFields and when the value of one is changed, all of the others also change to that value.

Can anyone explain this behavior?

• You are multiplying InputField[Dynamic@x] with the list n[[1]]. – Karsten 7. Jan 26 '15 at 5:15
• If for your application it might make sense to use p[1], p[2], ... instead of p1,p2, then all becomes easy – Rojo Jan 26 '15 at 6:05

I am not sure if this answer you, since not clear why you are doing this. btw calling the table as n is not good naming, changed to tab)

DynamicModule[{x = 5, tab},
Dynamic[Row[{
tab = Table[Symbol["p" <> ToString@i], {i, 1, x}];
InputField[Dynamic@x],
Dynamic@tab}
]]
]

• Thank you, this was very helpful! – Noah Rubin Jan 27 '15 at 5:19
DynamicModule[{x = 5,n},

Dynamic[
n = Table[Symbol["p" <> ToString@i], {i, 1, x}];
{InputField[Dynamic@x], Dynamic@n}
]]


or alternatively

DynamicModule[{x = 1, n = {p1}},

{InputField[Dynamic[x, (x = #; n = Table[Symbol["p" <> ToString@i], {i, 1, #}]) &]],
Dynamic@n}
]

• Very helpful, thank you! – Noah Rubin Jan 27 '15 at 5:19