title: How to prevent a callback from being interrupted by a figure's close function?
body: <p>Was wondering how the question in the title can be achieved. I have some callbacks that run from button presses. These callbacks, if interrupted by the figure close, will cause errors because the function seems to run, then gets interrupted by the close function which closes the figure, and then the callback seems to resume after the figure is closed. </p>
  
  <p>If I set the button's <code>'Interruptible'</code> property to <code>'on'</code>, it prevents other callbacks from interrupting it, but does not seem to work for the figure's close function. Another idea I had was to specify the <code>'closefunction'</code> in the figuring's <code>'CloseRequestFcn'</code> callback and then call <code>drawnow</code> to flush the eventqueue before deleting the figure but this did not work. </p>
  
  <p>The last resort for me is to simply set the figure's <code>'CloseRequestFcn'</code> to <code>''</code> when running callbacks but this seems tedious. Is there a standard solution to accomplish this? </p>
  
  <p>EDIT: </p>
  
  <p>From matlab's documentation:</p>
  
  <blockquote>
    <p>Note   If the interrupting callback is a DeleteFcn or CreateFcn
    callback or a figure's CloseRequest or ResizeFcn callback, it
    interrupts an executing callback regardless of the value of that
    object's Interruptible property. The interrupting callback starts
    execution at the next drawnow, figure, getframe, pause, or waitfor
    statement. A figure's WindowButtonDownFcn callback routine, or an
    object's ButtonDownFcn or Callback routine are processed according to
    the rules described above.</p>
  </blockquote>
  
  <p>So it appears to be the case that the <code>interruptible</code> property doesn't effect the close function. </p>
  
  <p>EDIT 2:</p>
  
  <p>Ok, so I think I found a problem. It's really bizarre. I actually discovered from the matlab documentation that callbacks are only interruptible if they have the <code>interruptible</code> property set to <code>on</code> AND :</p>
  
  <p><code>If there is a drawnow, figure, getframe, waitfor, or pause command in the running callback, then MATLAB executes the interrupting callbacks which are already in the queue and returns to finish execution of the current callback.</code></p>
  
  <p>I don't use any of these functions explicitly, so it turns out most of my callbacks aren't interruptible by the <code>closereqfcn</code>. BUT, it turns out some are, and the reasons why seem very strange. If have a callback with:</p>
  
  <p>`large computation -> imshow -> imshow</p>
  
  <p><code>large computation -&gt; set -&gt; set -&gt; set -&gt; set</code></p>
  
  <p>where the <code>set</code> command is setting the axes <code>visible</code> property to <code>off</code>, then no interruption seems to occur if I exit during the callback</p>
  
  <p>Now, if I have: </p>
  
  <p><code>large computation -&gt; imshow -&gt; set -&gt; imshow -&gt; set</code></p>
  
  <p>matlab issues an error if I exit during the callback on the second <code>set</code> command. Also, if I have:</p>
  
  <p><code>large computation -&gt; imshow -&gt; imshow -&gt; set</code></p>
  
  <p>matlab issues an error if I exit during the callback on the first <code>set</code> command. </p>
  
  <p><code>large computation -&gt; imshow -&gt; imshow -&gt; imshow</code></p>
  
  <p>also issues an error on the third <code>imshow</code> if I cancel during the callback.</p>
  
  <p>For some reason it seems that two successive calls to <code>imshow</code> makes my callback interruptible. Is it possible matlab implicitly calls <code>drawnow</code> or does something weird if you use multiple <code>imshow</code>s? BTW, my matlab version is R2009a.</p>