uitable in AppDesigner: cell edit callback and cell selection callback execute simultaneously
显示 更早的评论
Hello!
I'm creating a GUI with an editable table in ML2019. I want to select a part of that table and when I enter a new value, it should be written to all selected cells (just like one knows it from MS Excel or others).
To achieve this, I programmed a cell selection callback that stores the indices of the selected cells in a global variable that can be used in the cell edit callback function.
When I edit one of the selected cells and finish with <ENTER>, the cell edit callback is called, the eventdata argument holds the new value and the edited cell (and some other data) and my global variable holds the complete selection of cells. The new value is then copied to all selected cells, identified by my global variable. Everything's fine.
Now, when I do the same thing but finish the editing not with <ENTER> but by selecting another cell of the table, the new value is not written to the first selection but to the cell I selected to finish the editing. Meanwhile I found out, that both callback functions seem to be called simultaneously. But cellSelectCallback is faster, stores the new selection to my global variable and when the cellEditCallback uses the selection to write the new value, it gets the index of the new selection.
Is there a smarter way to avoid this behaviour? Maybe using global variables is not the best way but what else?
3 个评论
Voss
2025-3-19
I don't have R2019b, but I can confirm this behavior in R2016b, and I observe that the problem appears to be fixed by R2023b (i.e., clicking the table behaves the same as pressing <ENTER> (or <TAB>); btw, <TAB> works the same as <ENTER> in those two versions).
Here's the code I use to test:
function main_GUI()
f = uifigure();
t = uitable(f, ...
'Data',magic(4), ...
'ColumnEditable',true, ...
'CellSelectionCallback',@csc_fcn, ...
'CellEditCallback',@cec_fcn);
selected_cells = [];
function csc_fcn(~,evt)
selected_cells = evt.Indices;
end
function cec_fcn(~,evt)
for ii = 1:size(selected_cells,1)
t.Data(selected_cells(ii,1),selected_cells(ii,2)) = evt.NewData;
end
end
end
dpb
2025-3-19
Given @Voss's confirmation of the bug, it would seem upgrading would be the better solution, but to the "what else to do" in R2019b I note the following caveat in the <table properties callbacks documentation>
CellSelectionCallback — Cell selection callback function
function handle | cell array | character vector
Note
This property is not recommended for tables in App Designer or in apps created using the uifigure function. In uifigure-based apps, to execute commands when the app user selects different elements of the table, use the SelectionChangedFcn property instead.
Heiko
2025-3-20
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!