Is there a way to delete a whole row in a uitable by clicking a delete row button?

I would like to know if its possible to delete a row in a uitable (genereted in a GUI) by clicking a push button? thanks a lot for the help
p.s. I also would like to know if its possible to make up and down buttons, so the selected row can move up one row or down one row?

Answers (2)

When you create the uitable(), include a column for which ColumnEditable is true, and ColumnFormat is 'logical'. The uitable as a whole should have a CellEditCallback property. When the callback is invoked, the eventdata passed to the callback will be a structure; that structure fill have a field named Indices. When the indices match the row and column of one of your delete check boxes, set() the Data property to be the new cell array with that row deleted.

7 Comments

Walter,
Is another solution just saving the data and position, deleting the uitable, and creating a new one with the same coordinates? Or is this too hamfisted and too inefficient of a solution?
Your solution puts another column out for the user to see (unless you can make individual columns invisible?) I guess I would think there would be a a better way to delete rows from uitables.
It doesn't create an extra column. Basically you do this
% Get entire existing table.
tableData = get(handleToTable, 'Data');
% Now delete the row you don't want
tableData(rowToDelete) = []; % or something like that.
% Now send the shorter table back into the uitable.
set(handleToTable, 'Data', tableData);
Deleting the uitable and recreating it is inefficient and unnecessary: setting the Data property is all that is needed to remove any given row.
The additional column is one way to implement the push button that the poster requested. Another way would be to first select something in the row and then have a separate "delete" push-button that was not part of the uitable, with the callback using the information about what is selected in the uitable() in order to decide what to delete.
Note that there is no MATLAB-level access to information about what cell has been selected (it can be done at the Java level), so in order to know that, you need to program the uitable CellSellectionCallback to save that information somewhere accessible.
The "extra column" is in reference to my saying to "include a column for which ColumnEditable is true, and ColumnFormat is 'logical'" and use the CellEditCallback
imene benrabia comments to Image Analyst:
perfect
@ Walter and Image Analyst: Hope that you still remember this post. Currently, I want to delete a row of a UiTable with App Designer in Matlab 2018a. In your code below, what is 'handleToTable' and 'rowToDelete'? Which code I should write in CellEditCallback in order to recognize the row I want to delete?
Could you please provide more detail about this code section?
Thanks in advance!
% Get entire existing table.
tableData = get(handleToTable, 'Data');
% Now delete the row you don't want
tableData(rowToDelete) = []; % or something like that.
% Now send the shorter table back into the uitable.
set(handleToTable, 'Data', tableData);
handleToTable is intended to be the handle to the uitable being worked with.
You are wanting to work with App Designer. Using get() with App Designer should still work, but you would be more likely to code
tableData = app.AppropriateHandleName.Data;
rowToDelete is intended to be the index (or indices) of the rows to delete. My original response gave a mechanism for the user to indicate which rows to delete; the discussion after that moved more into the question of how to delete a row in the table when you knew which row "somehow" without having a checkmark for the user to indicate their choice.
With traditional figures you could take advantage of CellSelectCallback to have the user select a cell without a checkbox. However you still need an "activate" / "delete" button to confirm the user's choice... unless you want to delete any cell that the user clicks on (even accidentally.)

Sign in to comment.

If you want to delete table data with a pushbutton, then I think you'll have to also have a cell selection callback so you know what cell was last selected.
set(UITABLEHANDLE,'CellSelectionCallBack',@(h,e) set(h,'UserData',e))
Will make the indices of the last cell selected available in the user data of the table. Then, you can use that info for a pushbutton:
D=get(UITABLEHANDLE,'Data');
Index=get(UITABLEHANDLE,'UserData');
D(Index(1))=[];
set(UITABLEHANDLE,'Data',D);
There may well be a better way of doing it than this, but it's a start

6 Comments

You forgot to extract the Indices field from the eventdata.
(You might also want to take into account that multiple cells might be selected.)
1) Correct- spot what else I got wrong too...
D(Index.Indices(1),:)=[];
2) Yes- unique(Index.Indices(:,1)) would help.
Or don't worry about the unique:
D(Index.Indices(:,1), :) = [];
@ Tom: Hi Tom. In order to delete a row, Where should I put your below code? Should I put them in the push button callback function?
The UITBLEHANDLE should be written as app.uitable, correct? I am using App Designer of Matlab 2018a. Thanks a lot!
set(UITABLEHANDLE,'CellSelectionCallBack',@(h,e) set(h,'UserData',e))
D=get(UITABLEHANDLE,'Data');
Index=get(UITABLEHANDLE,'UserData');
D(Index(1))=[];
set(UITABLEHANDLE,'Data',D);
Updated: Following Tom's code, I succeeded to delete rows when selecting an arbitrary cell in the Table. I put the whole code inside Callback function of Delete Button.
But problem is that sometime when I click the Delete Button, there is an error:" Dot indexing is not supported for variables of this type." When I repeated do it for several times, the problem disappeared.
Is there anyone knowing the root of that error? Or is it a bug of Matlab 2018a, a version which I am using?
I applied my self the code and it's ok, it delete the row selected but when I want to add new row with other values it bring back also the deleted rows...
Do you know why?

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 10 Jun 2012

Commented:

on 23 May 2022

Community Treasure Hunt

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

Start Hunting!