1
$\begingroup$

I am trying to create a user interface for a demo project that uses matrix as an input. I provide user with a default matrix (as an example) for a start, but user can edit it or add extra row to it and then they could run the calculation on the input matrix by pressing Run button. I am getting the following error (shown in the figure below) whenever I open my document anew, but this error does not come when I run my cell once or twice. Error Error message

When the cell is run once or twice it should work like this

cell when it is run once or twice

Here is my code:

mat = ( {
 {21, 72, 3.0044},
 {0, 62.5, 3.5091},
 {1, 200, 3.6835}
} );
Column[{
 Dynamic[Table[
 With[{xi = i, yj = j}, 
  InputField[Dynamic[mat[[xi, yj]]], ImageSize -> 100]],
 {i, Dimensions[mat][[1]]}, {j, 3}] // TableForm],
Row[{Button["ADD ROW (+)", Dynamic[mat = Append[mat, {0, 0, 0}]], 
   ImageSize -> 100],
  Button["DEL. ROW (-)", 
   If[Dimensions[mat][[1]] > 1, mat = Delete[mat, -1]], 
   ImageSize -> 100]
   }], y = "Press Run";
 Button["Run", y = 2*mat // TableForm, ImageSize -> 200],
 Dynamic@y
}]

Note: In reality, operation on matrix is more complicated function than multiplying it twice (y = 2*mat). It is shown here for simplicity.

It would be helpful if one could provide solution to solve this problem concerning: updating or editing a matrix dynamically and then performing some operation on it after pressing 'Run' button.

Thank you in advance.

$\endgroup$
  • $\begingroup$ I did not use manipulate because the function on matrix is very complex and it makes it slow. I just want it to be done in steps to avoid overloading. $\endgroup$ – vdm1990 Apr 22 at 16:13
  • $\begingroup$ But when I am in between steps of editing a matrix, manipulate gives in between results that are not useful. So I want to divide input and output with a Run button. $\endgroup$ – vdm1990 Apr 22 at 16:18
  • 4
    $\begingroup$ What prevents you from adding a "Run" button to the Manipulate expression? $\endgroup$ – Lukas Lang Apr 22 at 16:23
1
$\begingroup$

Here is one solution using Manipulate, but one still needs to use Dynamic inside InputField for proper working.

Manipulate[
 Column[{
   Table[
    With[{xi = i, yj = j}, 
    InputField[Dynamic[mat[[xi, yj]]], ImageSize -> 100]],
   {i, Dimensions[mat][[1]]}, {j, 3}] // TableForm, y
  }],
   {{mat, ( {
    {21, 72, 3.0044},
   {0, 62.5, 3.5091},
   {1, 200, 3.6835}
    } )}, ControlType -> None},
   {{y, "Press Run"}, ControlType -> None},
  Button["ADD ROW (+)", mat = Append[mat, {0, 0, 0}], ImageSize -> 100],
   Button["DEL. ROW (-)", 
  If[Dimensions[mat][[1]] > 1, mat = Delete[mat, -1]], 
  ImageSize -> 100],
 Button["Run", y = 2*mat // TableForm, ImageSize -> 200]
  ]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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