How to deselect a cell on uitable using CellSelectionCallback?

23 views (last 30 days)
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 Comments
Adam Danz
Adam Danz on 21 Aug 2018
Nice. I posted the rest as an answer so that this question is marked as solved.
Noah
Noah on 27 Dec 2023
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;

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 21 Aug 2018
Edited: Adam Danz on 21 Aug 2018
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

More Answers (1)

Jonghoon Kim
Jonghoon Kim on 7 Jan 2022
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

Categories

Find more on Functions in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!