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: Re: Interrupting a callback function executing a 'while' loop
"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: Re: Interrupting a callback function executing a 'while' loop
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: Re: Interrupting a callback function executing a 'while' loop
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: Re: Interrupting a callback function executing a 'while' loop
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.
"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: Re: Interrupting a callback function executing a 'while' loop
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.
Public Submission Policy
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 Disclaimer prior to use.