1
$\begingroup$

I try to create a table which will describe traffic paths at a crossroads. Instead of using copy-paste repeatedly, I would like to write a loop.

For example:

Grid[{

    {Style["direction North", Bold]},
    For[i = 1, i < 4, i++, 
      {Style["traffic path", Bold], i, 

      Labeled[InputField[Dynamic[data[[i, 1]]], Hold[Expression],
      FieldSize -> 10], "Q", Left, LabelStyle -> Directive[Bold]], 

      Labeled[InputField[Dynamic[data[[i, 2]]], Hold[Expression], 
      FieldSize -> 10 ], "V", Left, LabelStyle -> Directive[Bold]],  

      PopupMenu[Dynamic[data[[i, 3]]], {1 -> "left", 2 -> "right",3 -> "straight "
               }](*popup*) 
      } (*style*)
    ](*For*) 
    }](*Grid*)

I do not have much experience in Mathematica, so this straightforwardly-written loop doesn't work. I have read the manuals, but haven't found any information.

Tell me, please, what I'm doing wrong!

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 29, 2015 at 17:47
  • $\begingroup$ the short answer to your actual question is to switch from For[i = 1, i < 4, i++ , ...code.. ] to Table[ ...code... , {i,3}]. Getting your Dynamic constructs to work is another matter.. $\endgroup$
    – george2079
    Jul 29, 2015 at 18:56
  • $\begingroup$ probably useful: mathematica.stackexchange.com/q/89433/2079 $\endgroup$
    – george2079
    Jul 29, 2015 at 19:19

2 Answers 2

1
$\begingroup$

The Table approach:

data = {{66, 55, 3}, {44, 33, 2}, {22, 11, 1}};
Grid[{{Style["direction North", Bold]}}~Join~
        Table[{Style["traffic path", Bold], i, 
          Labeled[InputField[Dynamic[data[[#, 1]]] &@i, FieldSize -> 10], 
           "Q", Left, LabelStyle -> Directive[Bold]], 
          Labeled[InputField[Dynamic[data[[#, 2]]] &@i, FieldSize -> 10], 
            "V", Left, LabelStyle -> Directive[Bold]], 
        PopupMenu[
               Dynamic[data[[#, 3]]] &@i, {1 -> "left", 2 -> "right", 
                 3 -> "straight "}]} , {i, 3}]]
$\endgroup$
1
  • $\begingroup$ +1 Dynamic[data[[#, 1]]] &@i - Nifty $\endgroup$ Jul 29, 2015 at 20:10
1
$\begingroup$

Try this:

data = {{66, 55, 3}, {44, 33, 2}, {22, 11, 1}};

MapIndexed[(d[First[#2]] = #[[1]]) &, data];
MapIndexed[(e[First[#2]] = #[[2]]) &, data];
MapIndexed[(f[First[#2]] = #[[3]]) &, data];

Grid[Prepend[Map[Function[i,
    {Style["traffic path", Bold], i,
     Labeled[InputField[Dynamic[d[i],
        (d[i] = data[[i, 1]] = #) &], FieldSize -> 10],
      "Q", Left, LabelStyle -> Directive[Bold]],
     Labeled[InputField[Dynamic[e[i],
        (e[i] = data[[i, 2]] = #) &], FieldSize -> 10],
      "V", Left, LabelStyle -> Directive[Bold]],
     PopupMenu[Dynamic[f[i], (f[i] = data[[i, 3]] = #) &],
      {1 -> "left", 2 -> "right", 3 -> "straight "}]}],
   Range[3]], {Style["direction North", Bold], Null, Null}]]

data
{{66, 55, 3}, {44, 33, 2}, {22, 11, 1}}

As values are changed in the input fields and popups the changes are reflected in data.

$\endgroup$
1
  • $\begingroup$ Thank you very much for the prompt and complete answer! $\endgroup$
    – Mishin V.
    Jul 30, 2015 at 13:38

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.