How can I name and manipulate several Input Fields? [duplicate]

I am completely new to mathematica--and have a very basic knowledge of programming. I would really, very much appreciate it if someone could explain to me how to refer to values entered into specific input fields when making calculations. I thought I could just name each input field something like a1 and a2 and then just use them to calculate values. Is my approach wrong? Here is what I have so far-- I am able to make a chart of blank input squares.

(My ultimate goal is to use particle charges and 1-dimensional distances in order to automatically calculate the energy between each particle)

a = {}
Panel[
Grid[
{
Prepend[
Table[
InputField[a,
FieldSize -> {w, h}], {h, {1, 1}}, {w, {4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}}],
Table["a"*i, {i, 20}]]
,
{"", "Chg", "Pos"}}
]   ]
]

values = Table[i*j, {i, 20}, {j, 20}];
label = Table["a"*i, {i, 20}];
labelwithvalues = Prepend[values, label ];
label2 = {"", label}
labelwithvaluesfinal =
Prepend, {labelwithvalues, {"", "a1", "a2", "a3", "a4", "a5", "a6",
"a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
"a16", "a17", "a18", "a19", "a20"}}];

-

marked as duplicate by C. E., Mike Honeychurch, m_goldberg, RunnyKine, ÖskåJul 17 '14 at 9:02

Esther, > is for quotes. There is a button next to the quote button that is for code (code is indented by four spaces). Using code formatting we don't have to use <br>, just so you know in the future. – C. E. Jul 16 '14 at 20:49
I might also add that programmatically generating input fields is pretty hard if you want them to work, and not a beginner's problem I would say. If you write the code for the input fields manually it would be much easier. InputField[Dynamic[a[1,1]] for example works. Now any code you write into that field will be put into a[1,1], thanks to Dynamic. – C. E. Jul 16 '14 at 21:17

This mess will get you something close to what you'd like, I'll explain the details below:

symbols = {"a1", "a2", "a3"};
Grid[Prepend[Transpose@
Prepend[Map[{InputField[ToExpression["Dynamic[" <> # <> "$Chg]"],String,FieldSize->{4,1}], InputField[ToExpression["Dynamic[" <> # <> "$Pos]"], String,FieldSize -> {4, 1}]} &, symbols], {"Chg", "Pos"}],
Prepend[symbols, ""]]]


Details:

InputField can be restricted to only format its content as a string (which you could think of as simply hiding string characters if you wish) with the second argument String - and can made to dynamically update a variable as follows:

InputField[Dynamic[myVar],String]


As wisely pointed out by Pickett in the comments above, programmatically generating these is not a beginners problem - you need to account for the evaluation order of Mathematica. In this case I've achieved that by StringJoin with the infix notation <> and then ToExpression the resultant String

Map[InputField[ToExpression["Dynamic[" <> # <> "\$suffix]"], String] &, {"var1", "var2"}]

-

The usual way to do this is to work with lists or arrays rather than try and create symbols. Here is a list version.

chg = ConstantArray[Null, 5];
pos = ConstantArray[Null, 5];

Grid[{
Join[{"Chg"},
Table[With[{i = i},
InputField[Dynamic[chg[[i]]], FieldSize -> 4]], {i,
Length[chg]}]],
Join[{"Pos"},
Table[With[{i = i},
InputField[Dynamic[pos[[i]]], FieldSize -> 4]], {i, Length[pos]}]]
}
]


You then enter values in the inout fields and use them for calculations. Without knowing what you are trying to do I can only provide a toy example:

-
+1. Do you have an explanation for the purpose of With[{i=i},...? – C. E. Jul 16 '14 at 22:38
Dynamic is HoldFirst. So you need to use With to inject i into the expression. – Mike Honeychurch Jul 16 '14 at 22:59
BTW I think this is probably a duplicate – Mike Honeychurch Jul 17 '14 at 0:06
Thanks for you answer, I hadn't seen that trick before. You are right, I was later able to find an answer about this trick here that can also be considered a duplicate of this post. – C. E. Jul 17 '14 at 0:52