MATLAB: Flush uicontrol callback queue

MATLABuicontrol callback multi thread

Hi, I want to show a continuously repeating animation that can be updated on the fly with pushbuttons. The problem is that a new function call cannot kill the previously running function. The function that was busy cannot kill itself since it was halted by the new call. another approach is to use one infinite loop and listen in that loop to changes made to the figure, but changes will never be made since the loop is busy. I cannot solve it with Busyaction/Interruptible. I tried to explain my example with the following piece of code. the buttons work 22 times and then the queue of interruptions is full. The buttons don't work anymore. In the real code the buttons work only 8 times. Is there a way to solve this? can i use some kind of threading to keep both processes running so that the old proces has time to kill itself?
function amimasteroramislave(varargin)
if ~nargin
clear all;close all;clc
it=1;
uicontrol('style','pushbutton','String','start slave 1 kill slave 2','FontSize',16,'HorizontalAlignment','center', ...
'Position',[10,10+40*(it-1),260,30],'Tag','masterbutton1','Callback',{@amimasteroramislave,1})%,'Interruptible','Off');

uicontrol('style','pushbutton','String','kill slave 1,start slave 2','FontSize',16,'HorizontalAlignment','center', ...
'Position',[10,10+40*(2-1),260,30],'Tag','masterbutton2','Callback',{@amimasteroramislave,2})%,'Interruptible','Off');
while 1
'Hello from master'
pause(1)
get(findall(0,'Tag','masterbutton1'),'Value')
get(findall(0,'Tag','masterbutton1'),'Selected')
end
else
while 1
['Hello from slave ' num2str(varargin{3})]
pause(1)
get(findall(0,'Tag','masterbutton1'),'Value')
get(findall(0,'Tag','masterbutton1'),'Selected')
end
end

Best Answer

Use a timer or multiple timers. They will handle this much better than while-loops. Don't forget to be liberal with the drawnow's either so that the queue can be flushed.