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:
Interrupting a callback function executing a 'while' loop

Subject: Interrupting a callback function executing a 'while' loop

From: Grace

Date: 30 Jan, 2008 08:17:04

Message: 1 of 10

I have made a GUI which has a 'Start' pushbutton, which
causes a 'while' loop to begin (pretty long loop). I also
have an 'Exit' pushbutton. I have tried many ways but on
pressing the Exit button, I am not able to make the program
stop running and the GUI to close without any error on
command window.

Could anyone please help me with that.

It is kind of urgent.

Thanks.
Grace

Subject: Interrupting a callback function executing a 'while' loop

From: Titus

Date: 30 Jan, 2008 11:14:40

Message: 2 of 10


"Grace " <gracee@mathworks.com> schrieb im Newsbeitrag
news:fnpbq0$rn4$1@fred.mathworks.com...
>I have made a GUI which has a 'Start' pushbutton, which
> causes a 'while' loop to begin (pretty long loop). I also
> have an 'Exit' pushbutton. I have tried many ways but on
> pressing the Exit button, I am not able to make the program
> stop running and the GUI to close without any error on
> command window.
>
> Could anyone please help me with that.
>
> It is kind of urgent.
>
> Thanks.
> Grace
>
>

Hi,
have the exit button set something in the handles structure (like
handles.pleaseStopNow = true;) and store it using guidata.
In your loop you need to add at the end of the loop:

drawnow; % this allows to be interrupted by exit button
handles=guidata(hObject);
if handles.pleaseStopNow
  % leave the loop
end

Titus

Subject: Interrupting a callback function executing a 'while' loop

From: Grace

Date: 30 Jan, 2008 17:10:26

Message: 3 of 10

Thanks for the reply. However it doesnt exit (or even change
the value of handles.pleaseStopNow) until I press the Exit
button rapidly for 2-3 times.

Could you suggest what can be wrong.

Thanks.
Grace


"Titus" <titus.edelhofer@mathworks.de> wrote in message
<fnpm71$daj$1@fred.mathworks.com>...
>
> "Grace " <gracee@mathworks.com> schrieb im Newsbeitrag
> news:fnpbq0$rn4$1@fred.mathworks.com...
> >I have made a GUI which has a 'Start' pushbutton, which
> > causes a 'while' loop to begin (pretty long loop). I also
> > have an 'Exit' pushbutton. I have tried many ways but on
> > pressing the Exit button, I am not able to make the program
> > stop running and the GUI to close without any error on
> > command window.
> >
> > Could anyone please help me with that.
> >
> > It is kind of urgent.
> >
> > Thanks.
> > Grace
> >
> >
>
> Hi,
> have the exit button set something in the handles
structure (like
> handles.pleaseStopNow = true;) and store it using guidata.
> In your loop you need to add at the end of the loop:
>
> drawnow; % this allows to be interrupted by exit button
> handles=guidata(hObject);
> if handles.pleaseStopNow
> % leave the loop
> end
>
> Titus
>
>

Subject: Interrupting a callback function executing a 'while' loop

From: Grace

Date: 30 Jan, 2008 19:40:21

Message: 4 of 10

Hi everybody

My program looks like this:

function Main_Fig_OpeningFcn(hObject, eventdata, handles,
varargin)
handles.output = hObject;
guidata(hObject, handles);
handles.StartStatus = 0;
guidata(hObject,handles)
handles.ExitStatus = 0;
guidata(hObject,handles)

function Start_button_Callback(hObject, eventdata, handles)
handles.StartStatus = 1;
guidata(hObject,handles)
while(xxx)
    .
    .
    .
    drawnow;
    if handles.ExitStatus == 1
        break;
    end
end
if handles.ExitStatus == 1
    delete(handles.output);
else
    ...
end

function Exit_button_Callback(hObject, eventdata, handles)
handles.ExitStatus = 1;
guidata(hObject,handles)
if handles.StartStatus == 0
    close all;
end


Could anybody please help me in making the Exit button work.

Thanks in advance.
Grace




"Grace " <gracee@mathworks.com> wrote in message
<fnqb22$p1c$1@fred.mathworks.com>...
> Thanks for the reply. However it doesnt exit (or even change
> the value of handles.pleaseStopNow) until I press the Exit
> button rapidly for 2-3 times.
>
> Could you suggest what can be wrong.
>
> Thanks.
> Grace
>
>
> "Titus" <titus.edelhofer@mathworks.de> wrote in message
> <fnpm71$daj$1@fred.mathworks.com>...
> >
> > "Grace " <gracee@mathworks.com> schrieb im Newsbeitrag
> > news:fnpbq0$rn4$1@fred.mathworks.com...
> > >I have made a GUI which has a 'Start' pushbutton, which
> > > causes a 'while' loop to begin (pretty long loop). I also
> > > have an 'Exit' pushbutton. I have tried many ways but on
> > > pressing the Exit button, I am not able to make the
program
> > > stop running and the GUI to close without any error on
> > > command window.
> > >
> > > Could anyone please help me with that.
> > >
> > > It is kind of urgent.
> > >
> > > Thanks.
> > > Grace
> > >
> > >
> >
> > Hi,
> > have the exit button set something in the handles
> structure (like
> > handles.pleaseStopNow = true;) and store it using guidata.
> > In your loop you need to add at the end of the loop:
> >
> > drawnow; % this allows to be interrupted by exit button
> > handles=guidata(hObject);
> > if handles.pleaseStopNow
> > % leave the loop
> > end
> >
> > Titus
> >
> >
>

Subject: Interrupting a callback function executing a 'while' loop

From: turtie

Date: 31 Jan, 2008 00:47:28

Message: 5 of 10

I have written a tutorial that discusses the problem that you are having, take a look.

http://www.blinkdagger.com/matlab/matlab-gui-tutorial-how-to-stop-a-long-running-function

Subject: Interrupting a callback function executing a 'while' loop

From: Grace

Date: 31 Jan, 2008 02:09:01

Message: 6 of 10

Thanks a lot for the tutorial. When I ran the tutorial, I
thought I will accomplish my task easily. However still it
doesn't exit until and unless I press it few times rapidly.

Do you have any idea what could the problem me.

Please let me know if you want any further information from me.

It is important to me since it is part of a project that I
have to submit.

Thanks.
Grace




turtie <qquach@gmail.com> wrote in message
<1685467.1201740478134.JavaMail.jakarta@nitrogen.mathforum.org>...
> I have written a tutorial that discusses the problem that
you are having, take a look.
>
>
http://www.blinkdagger.com/matlab/matlab-gui-tutorial-how-to-stop-a-long-running-function

Subject: Interrupting a callback function executing a 'while' loop

From: turtie

Date: 31 Jan, 2008 03:43:47

Message: 7 of 10

you can try replacing the drawnow command with pause(0.01), maybe that will help.

Subject: Interrupting a callback function executing a 'while' loop

From: turtie

Date: 31 Jan, 2008 03:46:39

Message: 8 of 10

Grace, if you contact me through the contact page from blinkdagger, I can take a look at your code. Hopefully it's something that can easily be fixed.

http://www.blinkdagger.com/contact

Subject: Interrupting a callback function executing a 'while' loop

From: Vihang Patil

Date: 31 Jan, 2008 05:12:01

Message: 9 of 10

"Grace " <gracee@mathworks.com> wrote in message <fnqjr5
$sro$1@fred.mathworks.com>...
> Hi everybody
>
> My program looks like this:
>
> function Main_Fig_OpeningFcn(hObject, eventdata, handles,
> varargin)
> handles.output = hObject;
> guidata(hObject, handles);
> handles.StartStatus = 0;
> guidata(hObject,handles)
> handles.ExitStatus = 0;
> guidata(hObject,handles)
>
> function Start_button_Callback(hObject, eventdata,
handles)
> handles.StartStatus = 1;
> guidata(hObject,handles)
> while(xxx)
> .
> .
> .
> drawnow;
> if handles.ExitStatus == 1
> break;
> end
> end
> if handles.ExitStatus == 1
> delete(handles.output);
> else
> ...
> end
>
> function Exit_button_Callback(hObject, eventdata,
handles)
> handles.ExitStatus = 1;
> guidata(hObject,handles)
> if handles.StartStatus == 0
> close all;
> end
>
>
> Could anybody please help me in making the Exit button
work.
>
> Thanks in advance.
> Grace
>
>
>
>
> "Grace " <gracee@mathworks.com> wrote in message
> <fnqb22$p1c$1@fred.mathworks.com>...
> > Thanks for the reply. However it doesnt exit (or even
change
> > the value of handles.pleaseStopNow) until I press the
Exit
> > button rapidly for 2-3 times.
> >
> > Could you suggest what can be wrong.
> >
> > Thanks.
> > Grace
> >
> >
> > "Titus" <titus.edelhofer@mathworks.de> wrote in message
> > <fnpm71$daj$1@fred.mathworks.com>...
> > >
> > > "Grace " <gracee@mathworks.com> schrieb im
Newsbeitrag
> > > news:fnpbq0$rn4$1@fred.mathworks.com...
> > > >I have made a GUI which has a 'Start' pushbutton,
which
> > > > causes a 'while' loop to begin (pretty long loop).
I also
> > > > have an 'Exit' pushbutton. I have tried many ways
but on
> > > > pressing the Exit button, I am not able to make the
> program
> > > > stop running and the GUI to close without any
error on
> > > > command window.
> > > >
> > > > Could anyone please help me with that.
> > > >
> > > > It is kind of urgent.
> > > >
> > > > Thanks.
> > > > Grace
> > > >
> > > >
> > >
> > > Hi,
> > > have the exit button set something in the handles
> > structure (like
> > > handles.pleaseStopNow = true;) and store it using
guidata.
> > > In your loop you need to add at the end of the loop:
> > >
> > > drawnow; % this allows to be interrupted by exit
button
> > > handles=guidata(hObject);
> > > if handles.pleaseStopNow
> > > % leave the loop
> > > end
> > >
> > > Titus
> > >
> > >
> >
>

Hello

You have to use this command
guidata(hObject, handles); in the while loop

while(xxx)
   .
   .
   .
    drawnow;
guidata(hObject, handles);
     if handles.ExitStatus == 1
         break;
    end
end
guidata(hObject, handles);

HTH
Vihang

Subject: Interrupting a callback function executing a 'while' loop

From: jim

Date: 10 Mar, 2008 01:45:06

Message: 10 of 10

I googled this earlier when searing searching for how to
exiting the program from the GUI


Viang has concept already, but you have to get the value
of the button, a 1 or 0, rather then read its handle. So if
exitstatus is the tag of the toggle button, then it would
be as below. Im not an expert by any means, but this
worked for me and its just a little modification..


    handles = guidata(HIgui); %make handles of gui visible
to this file
    exit_push = get(handles.ExitStatus, 'value') %find the
value, 0 if button not click, 1 if it is.
     if exit_push == 1 %exit program if clicked
         break;
     end

also found it slowed the program down by a factor of a 100
so only checked the button every 1000 loops so it want an
issue.

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