From: Mark on
Hi!

I am making a program with a large for loop, but I want the user to be able to stop the program at any given moment. I figured this code would be sufficient:

############################################
msg2 = msgbox('hello world','hello world'); %open a msgbox
for k = 0:50000 %start the for loop
k %execute some code, not relevant which
if ishandle(msg2) == 0
break;
end
end
#############################################

However, when I execute this, Matlab is very busy executing the forloop. So Matlab completely ignores the msgbox and doesn't delete it's handle when it is closed. If pressing the 'OK' button even closes the window, because somtimes Matlab even ignores that. So ishandle(msg2) == 0 is never true.

Adding "pause(1)" to the code between the two 'end'-statements resolves the problem, but I can't permit Matlab to pause for to long because of the rest of my code. Is there another way to make Matlab stop executing code or breaking from a forloop when the user wants this?

Thanks a lot for all help, I realy appreciate all comments!
From: Steven Lord on

"Mark " <markmatlab(a)gmail.com> wrote in message
news:hs934i$1b9$1(a)fred.mathworks.com...
> Hi!
>
> I am making a program with a large for loop, but I want the user to be
> able to stop the program at any given moment. I figured this code would be
> sufficient:
>
> ############################################
> msg2 = msgbox('hello world','hello world'); %open a msgbox
> for k = 0:50000 %start the for loop
> k %execute some
> code, not relevant which
> if ishandle(msg2) == 0 break;
> end
> end
> #############################################
>
> However, when I execute this, Matlab is very busy executing the forloop.
> So Matlab completely ignores the msgbox and doesn't delete it's handle
> when it is closed. If pressing the 'OK' button even closes the window,
> because somtimes Matlab even ignores that. So ishandle(msg2) == 0 is never
> true.
> Adding "pause(1)" to the code between the two 'end'-statements resolves
> the problem, but I can't permit Matlab to pause for to long because of the
> rest of my code. Is there another way to make Matlab stop executing code
> or breaking from a forloop when the user wants this?

Use DRAWNOW to give MATLAB a chance to check the event queue (and notice
that the msgbox has been deleted) inside the loop.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Mark on
Thanks, I will take a look and see if I can use that!

Are there other ways to make Matlab stop executing code when the user wants it? Like ctrl+c, but I want to make a GUI with it, so ctrl+c is not realy an option.