How do I set up a uicontrol callback to interrupt a routine?

11 views (last 30 days)
I would like to know how to set up a UIControl callback to interrupt a routine.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Mar 2020
Edited: MathWorks Support Team on 4 Mar 2020
In order to address this issue it is important to understand how MATLAB handles events. MATLAB does not automatically detect events as they occur. When events occur, they are placed into the 'event queue'. MATLAB stops the execution of a routine to look at the event queue only when one of a few commands are executed:
drawnow
figure (including gcf and gca)
getframe
waitfor
pause
'drawnow' is usually the command used to access the queue, since 'figure' has (probably undesirable) side effects (i.e. creating a figure), and 'getframe' takes longer to execute.
If the current process (when the event queue is examined) was issued from the command line, the process is suspended and the event queue is evaluated (i.e. callbacks occur). If the current process is itself a callback, the 'interruptible' property of the callback is consulted in order to decide whether or not it will be suspended in favor of event queue processing.
Here is a short example to illustrate these principles:
%%Example program -- can be run as a MATLAB script
h = uicontrol(gcf, 'style', 'pushbutton', ...
'callback', 'setappdata(gcf, ''run'', 0)');
flag = 1;
setappdata(gcf, 'run', flag);
disp('Entering loop');
n = 0;
while flag
n = n+1;
set(h,'string',num2str(n))
flag = getappdata(gcf, 'run');
drawnow
end
disp('Done with loop');

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!