4
$\begingroup$

I'm trying to build a pop up window with i lines. Each line would read:

Number of neurons in layer 1:
Number of neurons in layer 2:
etc until layer i

The user would enter a numeric value on each line. That value would be recorded as d[1],d[2],...,d[i].

The solution I've come up with results in Mathematica assigning the input from any line to d[i] (for example if there are 5 lines, it assign inputs from line 1, 2..5 all to d[5]). I understand why it's happening. I just can't think of how I'm suppose to achieve what I want.

Here's my code:

a = Table[
  List[StringJoin[{"Number of neurons in layer ", ToString[i], ":"}], 
   InputField[Dynamic[d[i]], Number]], {i, 3}]; 
AppendTo[a, {CancelButton[], DefaultButton[DialogReturn[]]}];
CreateDialog[Grid[a, Spacings -> {1, Automatic}, Alignment -> Left], Modal -> True];

Any suggestions? Thanks.

$\endgroup$
1
  • 1
    $\begingroup$ You need to change the first argument (List[...]) in Table to With[{i=i},List[...]]. Please check this answer to a related question. $\endgroup$
    – kglr
    Aug 28, 2012 at 23:20

1 Answer 1

3
$\begingroup$

Simple fix. Put With[{i=i}] to get the indexing in the dynamic right:

a = Table[List[
StringJoin[{"Number of neurons in layer ", ToString[i], ":"}],
With[{i = i}, InputField[Dynamic[d[i]], Number]]], {i, 3}];
AppendTo[a, {CancelButton[], DefaultButton[DialogReturn[]]}];

CreateDialog[Grid[a, Spacings -> {1, Automatic}, Alignment -> Left], Modal -> True];

On a sidenote, When I paste your code into Mathematica the i in InputField[Dynamic[d[i]], Number] turns red. If you highlight such a red character and go to the menu "help/why the coloring" it actually tells you that the order of evaluation conflicts.

I have added some more details on why this problem occurs, and why the solution works to a previous related question: Generating dynamic Inputfields

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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