Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to Mathematica.SE Omid! I formatted your code for you; here is a guide that shows how you can do this yourself on future posts. Also, please consider registering your account so that any upvotes you get on this question are added to those you might get on future questions and answers. That way, over time you will be able to do more on the site (post graphics, edit things, etc). –  Verbeia Aug 28 '12 at 23:01
1  
@Omid welcome to MMA.SE. You need to change the first argument (List[...]) in Table to With[{i=i},List[...]]. Pls check this answer to a related question. –  kguler Aug 28 '12 at 23:20
add comment

1 Answer 1

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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