How to deselect a cell on uitable using CellSelectionCallback?

26 Ansichten (letzte 30 Tage)
telmo egues
telmo egues am 30 Jul. 2018
Kommentiert: Noah am 27 Dez. 2023
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 Kommentare
Adam Danz
Adam Danz am 21 Aug. 2018
Nice. I posted the rest as an answer so that this question is marked as solved.
Noah
Noah am 27 Dez. 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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 21 Aug. 2018
Bearbeitet: Adam Danz am 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

Weitere Antworten (1)

Jonghoon Kim
Jonghoon Kim am 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

Kategorien

Mehr zu Functions finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by