3
$\begingroup$

I am looking for a way to enter an m*n matrix interactively using a dialog or a Graphical User Interface(GUI). Further, I would like to use this matrix later for computations.

So far, I have the following code:

lin = Input["Quantas Linhas ?: "];

col = Input["Quantas Colunas? : "];

a = {}; b = {}; nl = {};

m1 = 1;

While[m1 <= lin col, 
 AppendTo[a, Input["Elementos da Primeira Matriz:  "]]; m1++];

m2 = 1;

While[m2 <= lin col, 
 AppendTo[b, Input["Elementos da Primeira Matriz:  "]]; m2++];

matriz1 = Partition[a, col];
matriz2 = Partition[b, col];

MatrixForm[matriz1]



MatrixForm[matriz2]

i = 1;
k = 1;
While[k <= col, 
 While[i <= col, 
  AppendTo[nl, matriz1[[k]][[i]] + matriz2[[k]][[i]]] ; i++]; k++];
nl
$\endgroup$
2
  • 1
    $\begingroup$ It would have been more useful if you had added some verbal comments explaining the task at hand rather than a long title and a chunk of code $\endgroup$
    – bmf
    Mar 18, 2023 at 23:45
  • $\begingroup$ Have you see in the Toolbar under item "Insert" and "Table/Matrix" and then "New" . There you can input a matrix graphically. $\endgroup$ Mar 19, 2023 at 8:32

2 Answers 2

4
$\begingroup$

Here's an approach to present the user with an entire matrix of placeholders at once, which should make it easier to quickly enter a matrix without getting confused about which entry is being edited:

enter image description here

{m, n} = {2, 3};
DialogInput[{
  "Enter matrix:",
  Style[
   Framed[
    MatrixForm@Table[
      Style[
       RawBoxes@TagBox["\[Placeholder]", # &, BoxID -> "Placeholder"],
        Deployed -> False
       ],
      m, n
      ],
    Background -> White,
    BaseStyle -> {FontSize -> 12},
    ImageSize -> Full,
    FrameStyle -> LightGray
    ],
   Editable -> True
   ],
  ChoiceButtons[{
    DialogReturn[
     ToExpression@
      FirstCase[NotebookRead@Cells[][[2]], 
       FrameBox[g_, ___] :> g, $Failed, All]]
    }]},
 WindowSize -> {400, FitAll},
 ShowCursorTracker -> False,
 WindowTitle -> None,
 Selectable -> True, 
 Initialization :> Function[MathLink`CallFrontEnd[
    FrontEnd`BoxReferenceFind[
     FE`BoxReference[
      #,
      {{"Placeholder"}},
      FE`BoxOffset -> {FE`BoxChild[1]},
      FE`SearchStart -> "StartFromBeginning"
      ]
     ]
    ]
   ]
 ]

Notes:

  • The Selectable->True on the notebook and Deployed->False on the individual placeholders ensures that only the placeholders can be selected and edited.
  • The TagBox[...,BoxID->...] and FrontEnd`BoxReferenceFind trick is needed to select the first placeholder upon showing the notebook (this is based on what DialogInput does internally) - a normal NotebookFind did not work for me.
  • The ToExpression[...] part in the ChoiceButtons part selects the correct part of the notebook, and converts the entire matrix as an expression, making it usable for the rest of the code.
  • Most of the rest is styling, which can be adapted as desired. I based the options on those from DialogInput and tried to mimic the style of an InputField for the matrix input.
$\endgroup$
2
$\begingroup$

I do not use Input myself, but may be this could work for you

nRows = Input["Number of Rows? "];
nCols = Input["Numbers of Cols? : "];
mat = Table[0, {n, nRows}, {m, nCols}];

Do[
 Do[
  e = Input["Element (" <> ToString@n <> "," <> ToString[m] <> ")?"];
  mat[[n, m]] = e
  ,
  {n, 1, nRows}
  ]
 ,
 {m, 1, nCols}
 ]

enter image description here

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Input originates from Mathematica v1.0. Modal windows are, in general, especially for series of single inputs considered bad UI design. I'd consider TableView, Grid of InputFields, or such. This approach typically involves Dynamic, which is its own subject, though. $\endgroup$
    – kirma
    Mar 18, 2023 at 23:00
  • $\begingroup$ @kirma I think you meant this as comment to OP not me. I am simply using what they have. $\endgroup$
    – Nasser
    Mar 18, 2023 at 23:17
  • $\begingroup$ Yes, sort of, trying not to be too intimidating to a new contributor though. $\endgroup$
    – kirma
    Mar 18, 2023 at 23:25

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.