0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ It is possible but how to do it well depends on what do you want to do with it later. Do you want to reuse pv later or access pv1 directly? In many cases using a List or Association for pv would be good enough and more handy. $\endgroup$ – Kuba Jul 29 at 19:40
  • $\begingroup$ I don't need to reuse pv or access pv1. Since I am doing this in bulk, I only need to access the list by index to get at the dynamic values. How would the association work with Dynamic? Can you show some code on how that would work? Thanks! $\endgroup$ – user2253054 Jul 30 at 20:12
  • $\begingroup$ E.g.: PV = <||>; InputField[Dynamic[PV[#]], Number, FieldHint -> "Value", FieldSize -> 5] & /@ Range[5] it may introdce issues for large sets of values but should be fine for daily usage. $\endgroup$ – Kuba Jul 30 at 20:36
0
$\begingroup$

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]].

$\endgroup$

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.