Creating Graphical User Interfaces Previous page   Next Page

GUIDE M-Files and FIG-Files

GUIDE automatically generates two files for each GUI:

Note that if your GUI includes ActiveX components, GUIDE also generates a file for each ActiveX component. See ActiveX Controls for more information.

This topic discusses the following:

GUI File Creation

GUIDE creates the FIG-file and M-file the first time you save or run the GUI. You can save a GUI by selecting Save or Save as from the File menu, or by clicking the Save icon on the toolbar. You can run the GUI by selecting Run from the Tools menu or by clicking the Run icon on the toolbar. If you run the GUI without having saved it, GUIDE prompts you to save it.

When you save a GUI, GUIDE assigns the name you specify for the FIG-file to both the FIG-file and the M-file, for example, mygui.fig and mygui.m. When you click Save in the file-save dialog box, GUIDE saves both the FIG-file and the M-file, and then opens the M-file in the M-file Editor.

GUI M-File Structure

The GUIDE GUI M-file is a function file. The name of the main function is the same as the name of the M-file. Each callback in the file is a subfunction of the main function.

When GUIDE generates an M-file, it automatically includes templates for the most commonly used callbacks for each component. It also contains some initialization code, as well as an opening function callback and an output function callback. You must add code to the component callbacks for your GUI to work as you want. You may also want to add code to the opening function callback and the output function callback. The major sections of the GUI M-file are ordered as shown in the following table.

Section
Description
Comments
Displayed at the command line in response to the help command. Edit these as necessary for your GUI.
Initialization
GUIDE initialization tasks. Do not edit this code.
Opening function
Performs your initialization tasks before the user has access to the GUI.
Output function
Returns outputs to the command line before returning control to MATLAB.
Component callbacks
Control the behavior of individual components. MATLAB calls a callback in response to a particular event for a component or the figure itself.

Adding Callbacks to an Existing GUI M-File

Within GUIDE, you can add a callback subfunction to the GUI M-file in one of two ways:

Changing the Name of Your GUI

The name of your GUI is the same as the name of its FIG-file and M-file. To change the name of your GUI, you must change both these file names.

In the GUIDE Layout Editor File menu, use Save As to rename the GUI FIG-file. In response, GUIDE renames both the FIG-file and the M-file. GUIDE also updates the callback properties with the new file name, as well as all instances of the file name in the body of the M-file.

Converting a GUI to a Single M-File

You can convert a GUI that was created as a FIG-file and an M-file using GUIDE to a single M-file that does not require a FIG-file. This enables you to

You can perform this conversion by exporting your GUI using the following steps:

  1. Save the GUI in GUIDE, if you have not already done so.
  2. Select Export from the File menu.
  1. If you changed the GUI since you last saved it, this opens a dialog informing you that exporting will save changes to your figure and M-file, and asking if you want to continue.

  1. Save the exported M-file in the Save As dialog. By default, GUIDE gives the exported M-file the name of the GUI M-file with _export appended.


Previous page  GUI Files Files for Programmatically Created GUIs Next page

© 1994-2005 The MathWorks, Inc.