Is there a way to delete a whole row in a uitable by clicking a delete row button?
34 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Walter Roberson
2012-6-11
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 个评论
Nguyen Thuan
2020-8-4
编辑:Nguyen Thuan
2020-8-4
@ 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
2020-8-6
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.)
Tom
2012-7-3
编辑:Tom
2012-7-3
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 个评论
Nguyen Thuan
2020-8-11
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
2022-5-23
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!