title: On button press, break out of while loop? - MATLAB GUI
body: <p>I am having trouble breaking out of a while loop from an animated plot. Essentially, I have a MATLAB GUI with two buttons. One button starts an animated plot. The other button stops the animated plot. However, pressing the stop button does not break out of the while loop; it continues to plot. Is there something I am missing or is this simply not the way to go?  </p>
  
  <pre><code>% Infinite Loop
  i = 1;
  flag = true;
  
  % My attempt at breaking out of the while loop. ----------------------
  if get(handles.btnStopSim, 'Value') == 1
      flag = false;
  end
  %---------------------------------------------------------------------
  
  while flag
       % Update Point 
       set(hLine, 'XData', xInit(1, i), 'YData', yInit(1, i)) 
       set(hLine2, 'XData', xInit(2, i), 'YData', yInit(2, i))
       set(hLine3, 'XData', xInit(3, i), 'YData', yInit(3, i))
       set(hLine4, 'XData', xInit(4, i), 'YData', yInit(4, i))
       set(hLine5, 'XData', xInit(5, i), 'YData', yInit(5, i))
       set(hLine6, 'XData', xInit(6, i), 'YData', yInit(6, i))
       set(hLineTarget, 'XData', Target(1), 'YData', Target(2))
  
       drawnow
  
       pause(delay)
       i = rem(i + 1, numPoints) + 1;
       if ~ishandle(hLine), break;
       end
       if ~ishandle(hLine2), break;
       end
       if ~ishandle(hLine3), break;
       end
       if ~ishandle(hLine4), break;
       end
       if ~ishandle(hLine5), break;
       end
       if ~ishandle(hLine6), break;
       end
  end
  </code></pre>
  
  <p>The stuff in the while loop is just trajectory data. Standard stuff...</p>
  
  <p>Please let me know if more information is needed.</p>
  
  <p>Thank you!</p>