title: How to create a GUI inside a function in MATLAB?
body: <p>Is it possible to write a GUI from inside a function?</p>
  
  <p>The problem is that the callback of all GUI-functions are evaluated in the global workspace. But functions have their own workspace and can not access variables in the global workspace. Is it possible to make the GUI-functions use the workspace of the function? For example:</p>
  
  <pre><code>function myvar = myfunc()
      myvar = true;
      h_fig = figure;
  
      % create a useless button
      uicontrol( h_fig, 'style', 'pushbutton', ...
                        'string', 'clickme', ...
                        'callback', 'myvar = false' );
  
      % wait for the button to be pressed
      while myvar
          pause( 0.2 );
      end
  
      close( h_fig );
  
      disp( 'this will never be displayed' );
  end
  </code></pre>
  
  <p>This event-loop will run indefinitely, since the callback will not modify <code>myvar</code> in the function. Instead it will create a new <code>myvar</code> in the global workspace.</p>