Figures are the windows in which MATLAB displays graphics. Figures contain menus, toolbars, user-interface objects, context menus, axes and, as axes children, all other types of graphics objects.
MATLAB places no limits on the number of figures you can create. (Your computer systems might impose limitations, however.)
Figures play two distinct roles in MATLAB:
- Containing data graphs
- Containing graphical user interfaces
Figures can contain both graphs and GUIs components at the same time. For example, a GUI might be designed to plot data and therefore contain an axes as well as user interface objects.
[note]See Example — Using Figure Panels for an example of such a GUI.[/note]
The following diagram illustrates the types of objects that figures can contain.
Both figures and axes have children that act as containers. A uipanel can contain user interface objects and be parented to a figure and group objects (hggroup and hgtransform) can contain axes children (except light objects) and be parented to an axes.
See Objects That Can Contain Other Objects for more information.
Figures Used for Graphing Data
MATLAB functions that draw graphics (e.g., plot and surf) create figures automatically if none exist. If there are multiple figures open, one figure is always designated as the “current” figure, and is the target for graphics output.
The gcf command returns the handle of the current figure or creates a new figure if one does not exist. For example, enter the following command to see a list of figure properties:
The root object CurrentFigure > property returns the handle of the current figure, if one exists, or returns empty if there are no figures open:
get(0,'CurrentFigure') ans = 
See Controlling Graphics Output for more information on how MATLAB determines where to display graphics.
Figures that display graphs need to contain axes to provide the frame of reference for objects such as lines and surfaces, which are used to represent data. These data representing objects can be contained in group objects or contained directly in the axes. See Example — Transforming a Hierarchy of Objects for an example of how to use group objects.
Figures can contain multiple axes arranged in various locations within the figure and can be of various sizes. See Automatic Axes Resize and Multiple Axes per Figure for more information on axes.
Figures Used for GUIs
GUIs range from sophisticated applications to simple warning dialog boxes. You can modify many aspects of the figure to fit the intended use by setting figure properties. For example, the following figure properties are often useful when creating GUIs:
- Show or hide the figure menu, while displaying custom-designed menus (MenuBar).
- Change the figure title (Name).
- Control user access to the figure handle (HandleVisibility).
- Create a callback that executes whenever the user resizes the figure (ResizeFcn).
- Control display of the figure toolbar (Toolbar).
- Assign a context menu (UIContextMenu).
- Define callbacks that execute when users click drag or release the mouse over the figure (WindowButtonDownFcn, WindowButtonMotionFcn, WindowButtonUpFcn). See also the ButtonDownFcn property.
- Specify whether the figure is modal (WindowStyle).
See the Figure Properties reference page for more information on figure characteristics you can specify.
See the Creating Graphical User Interfaces documentation for more information about using figure to create GUIs.
Root Object — The Figure Parent
The parent of a figure is the root object. You cannot instantiate the root object because its purpose is only to store information. It maintains information on the state of MATLAB, your computer system, and some MATLAB defaults.
There is only one root object, and all other objects are its descendants. You do not create the root object; it exists when you start MATLAB. You can, however, set the values of root properties and thereby affect the graphics display.
[help]For more information, see Root Properties object properties.[/help]
More Information on Figures
See the figure reference page for information on creating figures.
See Callback Properties for Graphics Objects for information on figure events for which you can define callbacks.
See Figure Properties for information on other figure properties.