Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: GUI: Stop loops in another callback function
Replies: 3   Last Post: Apr 10, 2013 3:29 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Stanislava

Posts: 1
Registered: 4/10/13
Re: GUI: Stop loops in another callback function
Posted: Apr 10, 2013 3:29 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi,

I have a similar case of a gui with two pushbuttons and I tried your code. I am getting the error:
Undefined variable "data" or class "data.handles.figure1"
when the first button is pressed and the code reaches the line:
userData = get(data.handles.figure1, 'UserData')

This is the code for the first pushbtton callback:
keep_running=true;
n=0;
while keep_running
if n==1000
keep_running = false;
end

%also check if user click cancel
userData = get(data.handles.figure1, 'UserData')
if userData.stop
keep_running=false
end
n=n+1;
pause(1)
end

This is the code for the second one:
data = get(handles.figure1, 'UserData')
data.stop = true
set(handles.figure1, 'UserData',data)

I am quite new to MATLAB GUI and I would highly appreciate any help.
Thanks

"Nasser M. Abbasi" wrote in message <jiifdb$h3f$1@speranza.aioe.org>...
> On 2/28/2012 5:33 AM, Martinez wrote:
> > Hi
> >
> > I have a GUI with some parameters and a start and cancel button. When
> >I press the start button in the callback function of the start button,
> >some calculations are done with loops. When I press the cancel
> >button the calculations done in the start button callback should
> >stop immediately. How can I solve this without using a global variable like this:
> >
> > function start_callback(...)
> > global stop
> > stop=false;
> > N=1e6;
> > n=0;
> > while n<N&& ~stop
> > %calculations
> > n=n+1;
> > end
> >
> > function cancel_callback(...)
> > global stop
> > stop=true;
> >
> > Thanks
> > Martinez

>
> I'd use guidata.
>
> Your stop flag will be in the guidata buffer.
>
>
> Your 'start button' callback will become like this
>
> keep_running=true;
> while keep_running
> ....
> if n==N
> keep_running = false;
> end
>
> %also check if user click cancel
> userData = get(data.handles.figure1, 'UserData');
> if userData.stop
> keep_running=false;
> end
> end
>
>
> your cancel callback will turn the flag on
>
> data = get(handles.figure1, 'UserData');
> data.stop = true;
> set(handles.figure1, 'UserData',data);
>
> since callbacks can interrupt other callbacks, the above
> will work.
>
> --Nasser




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.