MATLAB Answers

0

Multiple editable tables in app designer

Asked by Irina Ion on 20 Oct 2019
Latest activity Answered by Katie
on 23 Oct 2019
Greetings, I'm using multiple editable tables in MATLAB's app designer and as soon as I try to edit the second one, all the values in the first table are reinitialized to 0.
Also, if I want to move on to insert other values in the second table, it does not hold the previous value either.
Any ideas?

  0 Comments

Sign in to comment.

1 Answer

Answer by Katie
on 23 Oct 2019
 Accepted Answer

Hi! I've made a couple apps with multiple editable tables, and here's how I've done it.
Disclaimer: my tables tend to be pretty small so I'm not really concerned with doing operations the most efficient way. I'm also typically pulling values out of my tables to use in calculations and then writing calculation results to other tables or plotting them. These two things drive my approach, but for your specific application there could be other drivers.
Steps:
  • Initialize the tables in the startup function
app.Table1.Data={0,0,0};
updateTable1(app);
app.Table2.Data={1,1,1};
updateTable2(app);
  • Create a celledit callback for each table. In the callback, I just put the updateTable_(app) function for the respective table.
function Table1CellEdit(app,event)
updateTable1(app);
end
  • Create a private update table function for each table. I either store the values in the table in properties (option 1) because I use them to do other calculations throughout the app or write property values to the table (option 2) because I've calculated a quantitity and I want to display it in the table.
function updateTable1(app)
table1Data=get(app.Table1,'Data')
%pick either option 1 approach or option 2 approach
%option 1:
app.val1=table1Data{1,1};
app.val2=table1Data{1,2};
app.val3=table1Data{1,3};
%option 2:
table1Data{1,1}=app.val1;
table1Data{1,2}=app.val2;
table1Data{1,3}=app.val3;
%write data to the table
app.Table1.Data=table5Data
end
  • I'm not 100% sure what you mean by insert values into your second table. If you mean add rows to your table here's what I do: To insert values into my tables, I have a button to add a table row. When the row is added, it is initialized with a set of values, then I use the appropriate updateTable_(app) function for the table to store all the values into properties again. After that, I can edit the fields in the new row of the table or the previous rows without anything happening to the other rows/cells. If by insert values into the second table you mean edit the cells, having the celledit callback function for the table with the updateTable_(app) function should take care of holding previous values.
%callback function for button push
function AddRowButtonPushed(app,event)
T=app.Table1.Data;
numRow=size(T,1)+1;
newData={0,0,0};%initialize new row
app.val1(numRow)=newData{1,1};
app.val2(numRow)=newData{1,2};
app.val3(numRow)=newData{1,3};
app.Table1.Data=[T;newData];%put the new data with the old data into the table
updateTable1(app);
end
Hope this helps! If not, if you could provide some more details and/or some code, I could try to recreate your isssue and work it further.

  0 Comments

Sign in to comment.