Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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 = {} 
      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"}}];
share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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. – Pickett 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. – Pickett Jul 16 '14 at 21:17

2 Answers 2

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

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

enter image description here


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:


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"}]
share|improve this answer

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

   Table[With[{i = i}, 
     InputField[Dynamic[chg[[i]]], FieldSize -> 4]], {i, 
   Table[With[{i = i}, 
     InputField[Dynamic[pos[[i]]], FieldSize -> 4]], {i, Length[pos]}]]

enter image description here

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:

enter image description here

share|improve this answer
+1. Do you have an explanation for the purpose of With[{i=i},...? – Pickett 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. – Pickett Jul 17 '14 at 0:52

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