code_junkie

How to create a GUI inside a function in Matlab?

Nov 12th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. function myvar = myfunc()
  2. myvar = true;
  3. h_fig = figure;
  4.  
  5. % create a useless button
  6. uicontrol( h_fig, 'style', 'pushbutton', ...
  7. 'string', 'clickme', ...
  8. 'callback', 'myvar = false' );
  9.  
  10. % wait for the button to be pressed
  11. while myvar
  12. pause( 0.2 );
  13. end
  14.  
  15. close( h_fig );
  16.  
  17. disp( 'this will never be displayed' );
  18. end
  19.  
  20. function make_useless_button()
  21.  
  22. % Initialize variables and graphics:
  23.  
  24. iCounter = 0;
  25. hFigure = figure;
  26. hButton = uicontrol('Style','pushbutton','Parent',hFigure,...
  27. 'String','Blah','Callback',@increment);
  28.  
  29. function increment(source,event)
  30. iCounter = iCounter+1;
  31. disp(iCounter);
  32. end
  33.  
  34. end
  35.  
  36. function make_stop_button()
  37.  
  38. % Initialize variables and graphics:
  39.  
  40. keepLooping = true;
  41. hFigure = figure;
  42. hButton = uicontrol('Style','pushbutton','Parent',hFigure,...
  43. 'String','Stop','Callback',@stop_fcn);
  44.  
  45. % Keep looping until button is pressed:
  46.  
  47. while keepLooping,
  48. drawnow;
  49. end
  50.  
  51. % Delete figure:
  52.  
  53. delete(hFigure);
  54.  
  55. % Nested callback:
  56.  
  57. function stop_fcn(source,event)
  58. keepLooping = false;
  59. end
  60.  
  61. end
  62.  
  63. function myfunc()
  64. h_fig = figure;
  65.  
  66. % add continue_loop to the GUI-handles structure
  67. fig_handles = guihandles( h_fig );
  68. fig_handles.continue_loop = true;
  69. guidata( h_fig, fig_handles );
  70.  
  71. % create a useless button
  72. uicontrol( h_fig, 'style', 'pushbutton', ...
  73. 'string', 'clickme', ...
  74. 'callback', @gui_callback );
  75.  
  76. % wait for the button to be pressed
  77. while fig_handles.continue_loop
  78. fig_handles = guidata( h_fig ); % update handles
  79. pause( 0.2 );
  80. end
  81.  
  82. close( h_fig );
  83. disp( 'callback ran successfully' );
  84. end
  85.  
  86. % The arguments are the Matlab-defaults for GUI-callbacks.
  87. function gui_callback( hObject, eventdata, handles )
  88. % modify and save handles-Structure
  89. handles.continue_loop = false;
  90. guidata( hObject, handles );
  91. end
Add Comment
Please, Sign In to add comment