Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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 11

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 11

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 11

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

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 11

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 11

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

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

Subject: Send an interrupt from a GUI

From: shabnam vcdsd

Date: 7 Nov, 2010 20:43:03

Message: 8 of 11

"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


Thats exactly my problem.

When I put a loop and check the button inside this, it consider it as an infinite loop and it doesn't check the button. it seems that when it is running a function for one button it ingnors all the buttons that are pressd.

Subject: Send an interrupt from a GUI

From: Michael

Date: 10 Nov, 2010 17:43:04

Message: 9 of 11

"shabnam vcdsd" <shabnam.koosha@yahoo.com> wrote in message <ib730n$ara$1@fred.mathworks.com>...
> "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
>
>
> Thats exactly my problem.
>
> When I put a loop and check the button inside this, it consider it as an infinite loop and it doesn't check the button. it seems that when it is running a function for one button it ingnors all the buttons that are pressd.

You may need to make your for loop interruptible. "If theInterruptible property of the object whose callback is executing is on, the callback can be interrupted. However, it is interrupted only when it, or a function it triggers, calls drawnow, figure, getframe, pause, or waitfor. " http://www.mathworks.com/help/techdoc/creating_guis/bq61qtj-1.html;jsessionid=Fl9XMhWMCQ6Z9gGWB2fCgHVQyjgFQvzgQdhJDh503Q4QmTLB21Gn!1797083685

If you have a huge loop, however, these may slow it down significantly.

Subject: Send an interrupt from a GUI

From: Michael

Date: 10 Nov, 2010 18:03:03

Message: 10 of 11

"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

I had this same problem and here's how I do it:

Initialize your stop button with this (paste into beginning of PLAY button callback):
set(handles.stopbutton,'UserData',0);

Then, in your PLAY button callback INSIDE your FOR loop, use this:
pause(0.00001)
if set(handles.stopbutton,'UserData') = 1
 break
end

Then, in your STOP button callback use this:
set(handles.stopbutton,'UserData',1);
guidata(hObject, handles);

Subject: Send an interrupt from a GUI

From: Walter Roberson

Date: 10 Nov, 2010 19:38:46

Message: 11 of 11

On 10-11-10 12:03 PM, Michael wrote:
> "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?

> I had this same problem and here's how I do it:
>
> Initialize your stop button with this (paste into beginning of PLAY
> button callback):
> set(handles.stopbutton,'UserData',0);
> Then, in your PLAY button callback INSIDE your FOR loop, use this:
> pause(0.00001)

You do not need to specify a time for pause. By specifying a time, you make it
wait at least that time, whereas if you do not specify the time, it would
return as soon as it finished checking whether there was anything in the queue.

> if set(handles.stopbutton,'UserData') = 1

That should be get() instead of set(), and should be == instead of = .

> break
> end
>
> Then, in your STOP button callback use this:
> set(handles.stopbutton,'UserData',1); guidata(hObject, handles);

You do not need the guidata() call, as you are not changing the value of
anything that is stored in the handles structure. handles.stopbutton will be a
handle (an arbitrary double precision number) that Matlab will use to look
inside its internal tables to find the structure that holds the data
associated with the handle. When you change that structure, the double
precision number used as the reference does not change, so the handles
structure would not need to be updated.

Tags for this Thread

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.

Contact us