How to delete a row in a editable table in an app?

Hello, I already programed an "add row" button but I don't know how to program a "delete row " button.I tried to copy the same function and add a - instead of a + but it does not work. I'll attach the captures with the code, including the FCN start up callback. Thank you

 채택된 답변

Mario Malic
Mario Malic 2023년 3월 5일
편집: Mario Malic 2023년 3월 7일
Hey,
idx = app.UITable.Selection(1);
app.UITable.Data(idx, :) = [];
Selection varies whether you have MultiSelect on or off. Check this document if you have MultiSelect on https://www.mathworks.com/help/matlab/ref/matlab.ui.control.table-properties.html#d124e1617327
Below is Walter's comment from the comment section.
%inside callback
fila = app.UITable.Selection;
if isempty(app.UITable.Data) || isempty(fila); return; end %table is empty or nothing is selected
app.UITable.Data(fila(1),:) = []; %delete what is selected

댓글 수: 10

Hi Mario, it didn't work. I'll show you the error
Make sure that the element is selected in the UITable.
What is the output of fila?
I programmed the callback to a button named "delete row". The output should be seen in the table, by deleting the row
I do not see an error message?
First make sure app.UITable.SelectionType = 'row'; (default is 'cell')
%inside callback
fila = app.UITable.Selection;
if isempty(fila); return; end %nothing is selected
app.UITable.Data(fila,:) = []; %delete what is selected
Hi walter, thank you, it works but it deletes all thw rows, not the empty ones only. What can I do?
Btw, the if you scroll to your right the image I attached, you ar able to read the error :)
Ah, that error indicates that the uitable is already empty.
%inside callback
fila = app.UITable.Selection;
if isempty(app.UITable.Data) || isempty(fila); return; end %table is empty or nothing is selected
app.UITable.Data(fila,:) = []; %delete what is selected
Nothing in the previous discussion indicated that you wanted to delete "empty" rows. The usage of this code is that you would first select one or more rows and then you would click on your button to trigger the deletion o the rows.
You're right, I want to delete a row (empty or not), but only one row at click. The code in the last answer you gave me deletes two rows when I click it, empty or not empty, how can I make it delete only one at a time?
%inside callback
fila = app.UITable.Selection;
if isempty(app.UITable.Data) || isempty(fila); return; end %table is empty or nothing is selected
app.UITable.Data(fila(1),:) = []; %delete what is selected
Thank you Walter it works perfectly :)!

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

추가 답변 (0개)

카테고리

제품

질문:

2023년 3월 5일

편집:

2023년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by