How to deselect a cell on uitable using CellSelectionCallback?

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 年 7 月 30 日
When I select a cell in a uitable the CellSelectionCallback is executed but when I enter a value in that cell and press enter, the CellSelectionCallback is not called again. Do you have a CellEditCallback function or some other function that might be calling your CellSelectionCallback?
telmo egues
telmo egues 2018 年 8 月 9 日
I've checked and i haven't, but the KeyPress function is in default with nothing written, maybe changing it in order to not re-enter CellSelection?
Adam Danz
Adam Danz 2018 年 8 月 9 日
If you attach the code for the GUI and explain which callback functions interact with the table I might be able to poke around later.
telmo egues
telmo egues 2018 年 8 月 21 日
Yes, here is the code
function uitable2_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable2 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
matriz = handles.dia;
indices = eventdata.Indices;
handles.edit2.String = num2str(matriz(indices(1),1));
handles.edit3.String = num2str(matriz(indices(1),2));
if matriz(indices(1),3) < 0
valor = matriz(indices(1),3) + 24*3600;
set(handles.checkbox1,'value',1);
else
valor = matriz(indices(1),3);
set(handles.checkbox1,'value',0);
end
horas = floor(valor/3600);
resto = rem(valor,3600);
minutos = floor(resto/60);
if minutos == 0
handles.edit4.String = strcat(num2str(horas),':','00');
else
handles.edit4.String = strcat(num2str(horas),':',num2str(minutos));
end
if matriz(indices(1),4) < 0
valor = matriz(indices(1),4) + 24*3600;
set(handles.checkbox2,'value',1);
else
valor = matriz(indices(1),4);
set(handles.checkbox2,'value',0);
end
horas = floor(valor/3600);
resto = rem(valor,3600);
minutos=floor(resto/60);
if minutos == 0
handles.edit5.String = strcat(num2str(horas),':','00');
elseif minutos <10
handles.edit5.String = strcat(num2str(horas),':','0',num2str(minutos));
else
handles.edit5.String = strcat(num2str(horas),':',num2str(minutos));
end
handles.edit6.String = num2str(matriz(indices(1),5));
handles.edit7.String = num2str(matriz(indices(1),6));
% set(hObject,'Enable','Off');
guidata (hObject,handles);
This is used in case the user selects a cell, a series of edits will display the information of the row in the matrix but if i change what's contained in the cell and press enter, it re-enters the CellSelection and gives an error without refreshing the value.
Adam Danz
Adam Danz 2018 年 8 月 21 日
編集済み: Adam Danz 2018 年 8 月 21 日
3 questions;
1 & 2) What is the error and which line is throwing the error?
3) When you press enter, does the active cell change to the next row (or column) within the UI table which might be triggering the cellSelectionCallback?
telmo egues
telmo egues 2018 年 8 月 21 日
1 & 2)The error says 'the index exceeds array bounds' and the error is located in
handles.edit2.String = num2str(matriz(indices(1),1));
3)When i press enter the active cell remains the same which is the cell selected firstly.
Adam Danz
Adam Danz 2018 年 8 月 21 日
編集済み: Adam Danz 2018 年 8 月 21 日
We're getting closer. Could you look into these two possibilities:
1) when you select the cell, everything works ok but when you press enter, the value of indices is empty. To test that, print out the value of 'indices' to the command window and select a cell, then press enter.
2) If possibility 1 is ruled out, check if the indices are correct after pressing enter but the value of 'matriz' has changed. If your cell selection is not changing then 'indices' should be the same values after pressing enter. The array bounds error could be due to the size of matriz changing. Perhaps print out matriz if it's not too big and see if it's changed between selecting the cell and pressing enter.
Lastly, let's make clear what your final goal is. In the title of your question, you ask to deselect a cell. But is your real goal just solving this problem?
telmo egues
telmo egues 2018 年 8 月 21 日
Just checked 1) and that is the problem, when pressing enter, the indices appear to be 0x2 and have no value. But that is not possible as the selection is mantained after the modification is done isn't it?
My real goal is solve this problem, i messed up with the title, sorry for that.
Adam Danz
Adam Danz 2018 年 8 月 21 日
Nice. I posted the rest as an answer so that this question is marked as solved.
Matlab Pro
Matlab Pro 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 日

1 投票

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

2 件のコメント

telmo egues
telmo egues 2018 年 8 月 22 日
Thanks for everything!
Andres Charris
Andres Charris 2024 年 6 月 21 日
Thanks, I have been the same erro for 2 years in my code.

サインインしてコメントする。

その他の回答 (1 件)

Jonghoon Kim
Jonghoon Kim 2022 年 1 月 7 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

製品

リリース

R2018a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by