TransWikia.com

Using Dynamic with a list of variables

Mathematica Asked by user2253054 on June 11, 2021

I am trying to generate a table of InputFields with a list of variables. I am generating the variable names on the fly as the lists I am dealing with have variable sizes.

len := 10
pv := Table[ToExpression["pv" <> ToString[n]], {n, len}]
ab := Table[
  With[{n = n}, 
   InputField[Dynamic[pv[[n]]],  FieldHint -> "Value", 
    FieldSize -> 5]], {n, len}]

This generates the table of input fields that I want. However, there are two issues:

  • When I try to change value of pv[[1]], (for example), directly rather than through the input field, I get an error: Set: Symbol pv in part assignment does not have an immediate value. I can chanage pv1 directly but not via the list index. I would like to be able to do this via the list index as I am handling big lists with variable number of columns and rows.
  • When the input field is generated it has the variable name in it. I have tried to override it with FieldHint. However that doesn’t work. I still get the variable name in the input field. I would like to have an empty input field displayed.

I have read the Dynamic and Control documentation, searched the web multiple times, etc. There is something basic that I am not understanding on the use of Dynamic here. Any help would be greatly appreciated.
Thanks

One Answer

With these small modifications, the code works for me even on a manual change of the pvs:

ClearAll[len, pv, ab];
len = 10;
pv = Table[Symbol["pv" <> ToString[n]], {n, len}];
ab = Table[
    With[{n = n}, InputField[
        Dynamic[pv[[n]]],
        FieldHint -> "Value",
        FieldSize -> 5
    ]],
    {n, len}
];

Notice the use of Symbol instead of ToExpression, and the use of Set instead of SetDelayed.

EDIT

Notice, also, that you must change the values of the variables pvN, not pv[[N]].

Answered by Shredderroy on June 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP