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

49 views (last 30 days)
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)

Walter Roberson
Walter Roberson on 11 Jun 2012
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
Nguyen Thuan
Nguyen Thuan on 4 Aug 2020
Edited: Nguyen Thuan on 4 Aug 2020
@ 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);
Walter Roberson
Walter Roberson on 6 Aug 2020
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.


Tom
Tom on 3 Jul 2012
Edited: Tom on 3 Jul 2012
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
Nguyen Thuan
Nguyen Thuan on 11 Aug 2020
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?
Cristian Martin
Cristian Martin on 23 May 2022
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 Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!