The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]
Nasser Abbasi

Posts: 6,677
Registered: 2/7/05
Re: GUI: Stop loops in another callback function
Posted: Feb 28, 2012 6:56 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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

while keep_running
if n==N
keep_running = false;

%also check if user click cancel
userData = get(data.handles.figure1, 'UserData');
if userData.stop

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.


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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.