Search code examples
matlabcallbackmatlab-uitable

Trigger CellEditCallback before Button Callback


I have a GUI with a uitable that the user can edit values in. I have a CellEditCallback function for that table that triggers and checks for input errors in the cells whenever a user presses enter while editing a cell or clicks outside the cell. That works great, but I also have a pushbutton that uses data from that table and my current problem is that when the pushbutton is clicked before any other spot outside the cell is clicked, or before enter is pressed for that matter, the pushbutton callback runs first, and after that callback finishes then the CellEditCallback runs. This is not ideal, as I need to check for errors before I use the data in my calculations. So, does anybody have any ideas on how to have the CellEditCallback function run first?

This code produces the problem I'm having:

% If you edit a cell and immediately click the button before clicking
% outside the cell or before hitting enter, the button's callback triggers
% before the CellEditCallback

function temp
% Create Figure
mainFig = figure('Units','characters',...
    'Position',[45 5 200 50],...
    'Renderer','opengl');

% Create uitable
tempData(1:10,1:5) = {''};
mainTable = uitable('parent',mainFig,...
    'Units','characters',...
    'Position',[5 25 180 20],...
    'ColumnEditable',[true],...
    'ColumnFormat',{'char'},...
    'ColumnWidth',{150 150 150 150 150},...
    'Data',tempData,...
    'CellEditCallback',@enterDataCallback);

% Create Button
mainButton = uicontrol('Parent',mainFig,...
    'Units','characters',...
    'Position',[5 10 180 10],...
    'Style','pushbutton',...
    'String','Button',...
    'Callback',@buttonCallback);

    % Function for when cell data is edited
    function enterDataCallback(src,evt)
        disp('Cell Edited')
    end

    % Function for when a button is pressed
    function buttonCallback(src,evt)
        disp('Button Pressed')
    end
end

Note 1: I did try using uiwait and waitfor but the problem isn't that the CellEditCallback function gets interrupted, it just is triggered after the pushbutton callback.

Note 2: That was a very basic description of what the functions do, but I do need the callbacks to trigger in that order because other things like flags and important variables in an outer function are set in the CellEditCallback so I need to have that callback run before the pushbutton one.

Thanks!


Solution

  • I contacted MATLAB Support about this problem and they told me that the callbacks occurring in that order is indeed an error and that it is fixed in the 2014b prerelease. However, to work around the error, I managed to do some messy coding to call the CellEditCallback from inside the Push Button Callback and then set a flag to make sure the CellEditCallback doesn't fire after the Push Button Callback is done.