Thread Subject: Send an interrupt from a GUI

Subject: Send an interrupt from a GUI

From: David Doria

Date: 11 Jul, 2008 16:29:02

Message: 1 of 7

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

Subject: Send an interrupt from a GUI

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 17:04:46

Message: 2 of 7

In article <g581oe$ic8$1@fred.mathworks.com>,
David Doria <daviddoria@gmail.com> wrote:
>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?

You cannot do that: there is no programmed equivilent to
pressing control-c.

See also the recent thread "COMMAND WINDOW INTERRUPT" of May 27th,

http://groups.google.ca/group/comp.soft-sys.matlab/browse_frm/thread/363221a0a5e915d/858f4ca6d85566ff
--
  "For men are prone to go it blind along the calf-paths of the
  mind To do what other men have done. They follow in the beaten
  track, and out and in, and forth and back, and still their
  devious course pursue, to keep the path that others do." -- Sam Walter Foss

Subject: Send an interrupt from a GUI

From: Gautam Vallabha

Date: 11 Jul, 2008 19:06:02

Message: 3 of 7

"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?

During your long-running function, periodically check to see
if the "STOP" button has been pressed. Here's an example of
how to do it:

-------------------------
function testAbort

figh = figure;
btn = uicontrol('style', 'pushb', 'string', 'Abort', ...
                'callback', @doAbort);
isAborted = false;

% simulate a long-running computation with PAUSE
for i=1:10
    checkForAbort();
    disp(i); pause(1);
end

%% =====
    function doAbort(h,e)
        isAborted = true;
    end

    function checkForAbort()
        if isAborted,
            error('Aborted');
        end
    end
end
-------------------------

Gautam

Subject: Send an interrupt from a GUI

From: David Doria

Date: 11 Jul, 2008 19:23:02

Message: 4 of 7

Well the problem is more that the loop that you call
"checkForAbort" from is in a separate m file and therefore
wouldn't have access to the button control. Do you see the
difference?

Thanks,

Dave

> function testAbort
>
> figh = figure;
> btn = uicontrol('style', 'pushb', 'string', 'Abort', ...
> 'callback', @doAbort);
> isAborted = false;
>
> % simulate a long-running computation with PAUSE
****
longFunction(); %unable to check button status from inside
this function!
***
>
> %% =====
> function doAbort(h,e)
> isAborted = true;
> end
>
> function checkForAbort()
> if isAborted,
> error('Aborted');
> end
> end
> end
> -------------------------
>
> Gautam
>

Subject: Send an interrupt from a GUI

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 19:29:14

Message: 5 of 7

In article <g58bum$9d5$1@fred.mathworks.com>,
David Doria <daviddoria@gmail.com> top-posted:

Please do not post your reply above what you are commenting on,
as it makes it difficult to hold a conversation.

>> function testAbort

>> figh = figure;
>> btn = uicontrol('style', 'pushb', 'string', 'Abort', ...
>> 'callback', @doAbort);
>> isAborted = false;

>> % simulate a long-running computation with PAUSE
>****
>longFunction(); %unable to check button status from inside
>this function!
>***

>> %% =====
>> function doAbort(h,e)
>> isAborted = true;
>> end
>>
>> function checkForAbort()
>> if isAborted,
>> error('Aborted');
>> end
>> end
>> end

>Well the problem is more that the loop that you call
>"checkForAbort" from is in a separate m file and therefore
>wouldn't have access to the button control. Do you see the
>difference?

Count the 'end' statements, Dave. Gautam used a nested function.
--
  "I will not approve any plan which is based on the old principle
  of build now and repair later." -- Walter Hickle

Subject: Send an interrupt from a GUI

From: Scott Burnside

Date: 11 Jul, 2008 21:21:01

Message: 6 of 7

"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

Subject: Send an interrupt from a GUI

From: Ron

Date: 26 Feb, 2009 20:36:01

Message: 7 of 7

Hi, here is an easy way:
Fist, in your function that take s along time, set a variable called stopper=0. In your loop always check to see if it is 0 or 1. If it is 1, then break.

In your gui stop button callback, use an
assignin('ws','stopper',1)

where ws is the workspace of the function (which can be set when you call the function).

 - Ron


"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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com