Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Send an interrupt from a GUI
Date: Fri, 11 Jul 2008 21:21:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 78
Message-ID: <g58irt$hm0$1@fred.mathworks.com>
References: <g581oe$ic8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1215811261 18112 172.30.248.37 (11 Jul 2008 21:21:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 11 Jul 2008 21:21:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869596
Xref: news.mathworks.com comp.soft-sys.matlab:478939



"David Doria" <daviddoria@gmail.com> wrote in message 
<g581oe$ic8$1@fred.mathworks.com>...
> I have a GUI with 2 buttons, "GO" and "STOP"
> 
> When I press "GO", a function is called that takes a long
> time to run.  I would like to make it so when I 
press "STOP"
> the execution is terminated (the same as pressing ctrl+c 
in
> the command window).  How do you do this?
> 
> Thanks,
> 
> Dave

Dave, you can drop this demo code into an m-file and try 
it. If your "long running function" is a loop you might be 
able to do something similar:

function rotate_triangle()
axes('units','normalized',...
     'position',[0.1 0.1 0.8 0.8],...
     'color',[0.5 0.5 0.5],...
     'NextPlot','replacechildren',...
     'tag','plot_axes');
hp = plot([1 3 2 1],[2 4 1 2]);
set(hp,'tag','tplot');
xlim([-8 8]);
ylim([-8 8]);
% define rotation button
uicontrol('units','normalized',...
    'position', [0.42 .925 .15 .05],...
    'style','push',...
    'SelectionHighlight','off',...
    'string','rotate',...
    'fontweight','bold',...
    'fontsize', 10,...
    'fontname','arial',...
    'foregroundcolor',[0 0 0],...
    'callback',{@rotate_button},...
    'tag','rotate_button');

function rotate_button(hload,eventdata)
% locate and delete the main figure object
ax = findobj('tag','plot_axes');
hp = findobj('tag','tplot');
hb = findobj('tag','rotate_button');
stop_flag = get(hp,'userdata');
if ~isempty(stop_flag)
    if stop_flag == 0
        set(hp,'userdata',1)
        set(hb,'string','rotate');
    elseif stop_flag == 1
        set(hp,'userdata',0);
        set(hb,'string','stop');
    end
else
    set(hp,'userdata',0)
    set(hb,'string','stop');
end
t = hgtransform('Parent',ax);
set(hp,'Parent',t)
Rz = eye(4);
for r = 0:.1:2000*pi
    Rz = makehgtform('zrotate',r);
    set(t,'Matrix',Rz)
    drawnow
    pause(0.01)
    stop_flag = get(hp,'userdata');
    if stop_flag == 1
        break
    end
end
return
% end of code

hth,
Scott