MATLAB Answers

add row to table in app designer - works ... but not completely

조회 수: 69(최근 30일)
LO
LO 2020년 7월 7일
댓글: Adam Danz 2020년 7월 7일
I am trying to add rows to a table by clicking a button in a GUI
the code is as follows: the table is first initialized through a first button and then the second button is supposed to fill in data (I have not found a better way to add the row of data without this initialization. It seems that after the first addition the second row fails to concatenate, although the values should be taken from the same edit boxes which are constantly updated - I can see that actually)
function ButtonPushed(app, event)
emptydata = {0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 };
set(app.UITable, 'Data', emptydata);
end
...
function ButtonPushed(app, event)
rowdata = {app.EditField2.Value app.EditField.Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 };
% app.UITable = [app.UITable; rowdata];
app.UITable.Data = [{app.UITable.Data{:}}; rowdata];
end
  댓글 수: 1
dpb
dpb 2020년 7월 7일
I've never used UITable so no real idea how it works, but looks to me like
app.UITable.Data = [{app.UITable.Data{:}}; rowdata];
would be trying to catenate a cell containing a row vector to a cell of the existing table .Data content that would be a 2x1 cell array, not the catenation of the .Data content itself.
I dunno how the UITable stores the .Data property exactly...surely there are examples?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 7월 7일
If the uitable already has 18 columns,
Follow the example shown here.
app.UITable.Data = {0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0};
Then add rows, again with 18 columns,
rowdata = {2, 5, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0};
app.UITable.Data = [app.UITable.Data; rowdata];
This has been tested in r2020a.
If the uitable does not have 18 columns, then the first line should be replaced with
app.UITable.Data = num2cell([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]);
Now it will have 18 columns and the 2nd line can stay the same (as long it is also produces 18 columns of data).
  댓글 수: 2
Adam Danz
Adam Danz 2020년 7월 7일
The curly bracket syntax requires that the table already has the same number of columns as the row of values. If your UITable will always contain the same number of columns, you could set that up in AppDesigner > Design View.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by