Interrupt current script when closing message bar

I use waitbar as follows:

h = waitbar(0,'Please wait...'); for i=1:100, % computation here % waitbar(i/100) % other operation here end close(h) 

I would like to stop this script if the user closes the wait window (presses the X button of the window) without adding the Cancel button.

Is there any way to do this?

+6
source share
2 answers

You can check if h valid descriptor, and otherwise exit the loop. Insert the following into your loop:

 if ~ishandle(h) break end 
+5
source

You can try something like this:

 if ishandle(h), close(h); % Your code here else %waitbar has been closed by the user % call throw, return, or break end 

Hope this helps,

+1
source

All Articles