Modifying data in uitable causes CellSelectionCallback to execute and throw error -How do I prevent this?

3 views (last 30 days)
I have a UI table which populates other UI tables with data based on which row is currently selected, using the CellSelectionCallback function. Unfortunately, when I modify or remove rows in this master UI table, MATLAB appears to execute the CellSelectionCallback function, which throws an error at one of my conditional statements. I suspect that MATLAB is deselecting whatever cell had been selected previously and this is causing the eventdata.Indices variable to become empty or no longer in a recognized format, but my efforts to put corrective conditional statements (isempty, isnull, is < 1) in there to catch this have failed to fix the problem. I've tried putting debug breakpoints in the CellSelectionCallback function to see what's going on, but for some reason MATLAB ignores them. This question was asked a number of years ago, but apparently never answered. Anyone know what kind of conditional statements I could put in the SelectionCallback that could catch this issue? Many thanks in advance!
The error message MATLAB generates is below. tab_Category is the name of the uitable, and the "if all..." function on line 97 is my conditional statement for performing normal GUI tasks:
Index exceeds matrix dimensions.
Error in KeywordInputDiag>tab_Category_CellSelectionCallback (line 97) if all(~cellfun(@isempty,temp(eventdata.Indices(1),1:2)))
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in KeywordInputDiag (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)KeywordInputDiag('tab_Category_CellSelectionCallback',hObject,eventdata,guidata(hObject))
Error while evaluating uitable CellSelectionCallback

Accepted Answer

Michael B
Michael B on 15 Apr 2016
After playing around with the script I managed to figure out what was going on. When something causes the current table selection to be cleared, it automatically triggers the CellSelectionCallback function and runs any code inside. The eventdata.Indices variable that is generated by this action is a 0x2 double array. Performing an isempty check on this directly will return 0 because eventdata.Indices isn't empty, it's just an array of zero length. If anything within the CellSelectionCallback function uses the value of eventdata.Indices and doesn't have a way to handle a zero-length array as input, MATLAB will throw an error. This can be avoided simply by adding a conditional statement inside the CellSelectionCallback function that checks the length of eventdata.Indices before doing anything else.
  1 Comment
joe
joe on 18 Jan 2018
Hallo Mihael B,
I have the same Problem but i i dont know how to solve it.
my code Looks like this:
function table_Diagrams_CellSelectionCallback(hObject, eventdata, handles)
row = eventdata.Indices(1);
col = eventdata.Indices(2);
assignin('base','row',row)
assignin('base','col',col)
i will be grateful, if you can help me
thank you in advance

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!