# Entering matrices interactively

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

• 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
– bmf
Mar 18 at 23:45
• Have you see in the Toolbar under item "Insert" and "Table/Matrix" and then "New" . There you can input a matrix graphically. Mar 19 at 8:32

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:

{m, n} = {2, 3};
"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@
FrameBox[g_, ___] :> g, \$Failed, All]]
}]},
WindowSize -> {400, FitAll},
ShowCursorTracker -> False,
WindowTitle -> None,
Selectable -> True,
Initialization :> Function[MathLinkCallFrontEnd[
FrontEndBoxReferenceFind[
FEBoxReference[
#,
{{"Placeholder"}},
FEBoxOffset -> {FEBoxChild[1]},
FESearchStart -> "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 FrontEndBoxReferenceFind 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.

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


• 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. Mar 18 at 23:00
• @kirma I think you meant this as comment to OP not me. I am simply using what they have. Mar 18 at 23:17
• Yes, sort of, trying not to be too intimidating to a new contributor though. Mar 18 at 23:25