Creating and Running the GUI M-File

File Organization

Typically, a GUI M-file has the following ordered sections. You can help to maintain the organization by adding comments that name the sections when you first create them.

  1. Comments displayed in response to the MATLAB® help command.

  2. Initialization tasks such as data creation and any processing that is needed to construct the components. See Initializing the GUI for more information.

  3. Construction of figure and components. For more information, see Creating the GUI Figure and Adding Components to the GUI.

  4. Initialization tasks that require the components to exist, and output return. See Initializing the GUI for more information.

  5. Callbacks for the components. Callbacks are the routines that execute in response to user-generated events such as mouse clicks and key strokes. See Programming the GUI for more information.

  6. Utility functions.

File Template

This is a template for a GUI M-file:

function varargout = mygui(varargin)
% MYGUI Brief description of GUI.
%       Comments displayed at the command line in response 
%       to the help command. 

% (Leave a blank line following the help.)

%  Initialization tasks

%  Construct the components

%  Initialization tasks

%  Callbacks for MYGUI

%  Utility functions for MYGUI

end

The end statement that matches the function statement is necessary because this document treats GUI creation using nested functions. Programming the GUI addresses this topic.

Save the file in your current directory or at a location that is on your MATLAB path.

Running the GUI

You can display your GUI at any time by executing its M-file. For example, if your GUI M-file is mygui.m, type

mygui

at the command line. Provide run-time arguments as appropriate. The files must reside on your path or in your current directory.

When you execute the GUI M-file, a fully functional copy of the GUI displays on the screen. You can manipulate components that it contains, but nothing happens unless the M-file includes code to initialize the GUI and callbacks to service the components. Programming the GUI tells you how to do this.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS