1
$\begingroup$

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?

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

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)

enter image description here

DynamicModule[{x = 5, tab},
 Dynamic[Row[{
    tab = Table[Symbol["p" <> ToString@i], {i, 1, x}];
    InputField[Dynamic@x],
    Dynamic@tab}
   ]]
 ]
$\endgroup$
  • $\begingroup$ Thank you, this was very helpful! $\endgroup$ – Noah Rubin Jan 27 '15 at 5:19
1
$\begingroup$
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}
 ]
$\endgroup$
  • $\begingroup$ Very helpful, thank you! $\endgroup$ – Noah Rubin Jan 27 '15 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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