title: Add button to MATLAB figure (not GUI) to exit loop
body: <p>I have created a script that enters a while loop acquiring incoming COM port data, and also produces a figure that updates with said data.</p>
  
  <p>Currently, I do not have an elegant way to exit this loop. I would like to be able to add a button of sorts to accomplish this, but my knowledge of the subject is hazy.</p>
  
  <p>I do not really want to create a GUI, wondering if I can slap a button on my figure.</p>
  
  <p>What I have tried:</p>
  
  <p>Created a separate m-file to house a button-press call-back to get the button state:</p>
  
  <pre><code>function [button_state] = fun1(hObject, eventData)
      button_state = get(hObject, 'Value');
  end
  </code></pre>
  
  <p>Within the while loop I have the following button declaration, where 'fig' is my figure plot:</p>
  
  <pre><code>while 1 &gt; 0
  
      ...
  
      fig = figure(1);
  
      ...
  
      tb = uicontrol(fig, 'Style', 'togglebutton', 'String', 'Stop', 'Callback', @fun1);
  
      out = fun1(tb);
      if out == 1
          break;
      end
  
  end
  </code></pre>
  
  <p>Doesn't seem to work... Any help is much appreciated. Thanks.</p>