How to deselect a cell on uitable using CellSelectionCallback?

26 次查看(过去 30 天)
Hi, Tried several solutions found in the forum but none of them worked, in my program, when a cell is selected, all the info of the row is displayed in different edit cases, when the cell in the uitable is edited and enter is pressed, the wanted value changes properly but the CellSelectionCallback is called again and the indices this time are inexistent and returns an error. Any way of fixing this? Thanks in advance
  10 个评论
Adam Danz
Adam Danz 2018-8-21
Nice. I posted the rest as an answer so that this question is marked as solved.
Noah
Noah 2023-12-27
Hi - this simples way to do is just to "refresh" the "Data" field;
% Refresh the "Data" field" to its own = Unselect
handles.my_tbl.Data = handles.my_tbl.Data;

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2018-8-21
编辑:Adam Danz 2018-8-21
Following the comments under the question, we've arrived to the answer.
For some reason, pressing enter after entering data in a UItable activates your cellselectionCallback (which isn't the case when I try it using a simple UI table in 2018a).
When your cellSelectionCallback is activated after pressing enter, the indices are empty.
The real solution would be to prevent the unnecessary callback in the first place but without playing around with your entire GUI I cannot do that remotely.
The alternative solution is to escape from the callback function when the indices are empty.
Insert this at the top of your cellSelectionCallback function.
if isempty(eventdata.Indices)
return
end

更多回答(1 个)

Jonghoon Kim
Jonghoon Kim 2022-1-7
function [] = DeselectCellinUItable()
%---------------------------------------------------------------
uif = uifigure();
uif.Units = 'normalized';
uif.Position = [0.10, 0.50, 0.80, 0.40];
%---------------------------------------------------------------
uit = uitable(uif);
uit.Units = 'normalized';
uit.Position = [0.10, 0.50, 0.80, 0.40];
uit.Data = cell2table(num2cell(rand(10,7)));
uit.RowName = 'numbered';
uit.ColumnEditable = true;
uit.SelectionType = 'cell';
uit.Multiselect = 'off';
uit.SelectionChangedFcn = @(src,event) DeselectUItable;
%---------------------------------------------------------------
function DeselectUItable
uit.Selection = [];
end
%---------------------------------------------------------------
end

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by