MATLAB: Does the UICONTROL callback act differently in MATLAB 7.7 (R2008b) when I click it rapidly

MATLAB

I have a GUI which uses a slider control. The callback gets the 'Value' property of the slider and then does some processing which takes some amount of time. In MATLAB R2008a, when I click the slider button rapidly the callbacks queue up as expected, and the 'Value' returned in each subsequent callback increments by 1 (five clicks yield 'Values' if 1, 2, 3, 4, 5 in subsequent callback executions). However in MATLAB R2008b, when I click the slider button rapidly, the callbacks still queue up properly but sometimes it skips values (five clicks yield 'Values' of 1, 3, 5, 5, 5).

Best Answer

This is due to a change in the way that the MATLAB execution thread (which manages the callback executions) and the Java event thread (which manages updating the control and it's 'Value' property) work together. The change was made to make GUIs be more responsive.
A workaround to simulate the old behavior is to disable the slider at the beginning of the callback and re-enable at the end of the callback. This way, any clicks that occur while the last callback execution is still processing are discarded.
function main
u = uicontrol( ...
'Callback', @callback, ...
'Style', 'slider', ...
'Min', 0, ...
'Max', 100, ...
'Interruptible', 'on', ...
'BusyAction', 'queue');
end
function callback(src, evt)
fprintf('Callback started. \n');
% Disable the control and use DRAWNOW to force the change to go
% through before the processing starts. DRAWNOW UPDATE just updates the
% slider (and other UI controls) without effecting the event queue.
set(src, 'Enable', 'off');
drawnow update;
% Print slider value to command window
b = get(src, 'Value');
fprintf('%d\n',b);
% Waste some time
a = 0;
for i = 1:10e7, a = a + 1; end
% Re-enable the control
set(src, 'Enable', 'on');
fprintf('Callback finished. \n\n');
end