How can I kill/flush callbacks queued in a GUI environment?

2 views (last 30 days)
I recreated the problem using the sample code below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[] = sample_gui()
S.fh = figure('units','pixels', 'position', [700, 700, 500, 500]);
S.button_1 = uicontrol('Style', 'pushbutton', 'String', 'BUTTON_1', 'Position', [20 200 100 20], 'Callback', @button_1_call);
S.button_2 = uicontrol('Style', 'pushbutton', 'String', 'BUTTON_2', 'Position', [20 100 100 20], 'Callback', @button_2_call);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[] = button_1_call(varargin)
fprintf('Button 1 was pressed\n');
for p = 1:5
button_press = true;
while button_press
what_is_clicked = waitforbuttonpress;
if what_is_clicked > 0, button_press = false; end
end
character = get(gcf, 'CurrentCharacter');
fprintf('%d of 5 pressed %s\n', p, character);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[] = button_2_call(varargin)
fprintf('Button 2 was pressed\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Note that button 1 callback function executes for a fixed number of iterations (5 in this case) and CAN be interrupted by button 2. When I launch this GUI and execute button presses in the same order: button 1, I execute just three of the five iterations, interrupt by button 2, then again press button 1 and execute all five iterations.
Desired Output
----------------------------
Button 1 was pressed
1 of 5 pressed a
2 of 5 pressed b
3 of 5 pressed c
Button 2 was pressed
Button 1 was pressed
1 of 5 pressed a
2 of 5 pressed b
3 of 5 pressed c
4 of 5 pressed d
5 of 5 pressed e
Observed output
----------------------------
Button 1 was pressed
1 of 5 pressed a
2 of 5 pressed b
3 of 5 pressed c
Button 2 was pressed
Button 1 was pressed
1 of 5 pressed a
2 of 5 pressed b
3 of 5 pressed c
4 of 5 pressed d
5 of 5 pressed e
4 of 5 pressed e
5 of 5 pressed f
What bothers me is the last two lines, these are residue from my previous press of button 1. I have tried drawnow, drawnow update expose, break, return, refresh, BusyAction - 'cancel', etc in all possible permutations. None of these statements causes the for loop to exit.

Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!