Adding Titles to Graphs

What Is a Title?

In a MATLAB figure, a title is a text string at the top of an axes. It appears in the figure border, not within the axes it describes. Titles typically define the subject of the graph. The following figure shows a title, centered at its top.

You can add a title to a graph in several ways, described in the following sections.

Using the Title Option on the Insert Menu

To add a title to a graph using the Insert menu,

  1. Click the Insert menu in the figure menu bar and choose Title. A text entry box opens at the top of the axes.

  2. Enter the text of the label.

  3. When you are finished entering text, click anywhere in the figure background to close the text entry box around the title. If you click on another object in the figure, such as an axes or line, you close the title text entry box and also automatically select the object you clicked.

To change the font used in the title to bold, you must edit the title. You can edit the title as you would any other text object in a graph.

Using the Property Editor to Add a Title

To add a title to a graph using the Property Editor,

  1. Start plot editing mode by selecting Edit Plot from the figure Tools menu.

  2. Double-click an empty region of the axes in the graph. This starts the Property Editor. You can also start the Property Editor by right-clicking on the axes and selecting Show Property Editor from the context menu or by selecting Property Editor from the View menu.

    The Property Editor displays a property panel specific to axes objects. Titles are a property of axes objects.

  3. Type the text of your title in the Title text entry box.

You can change the font, font style, position, and many other aspects of the title format.

Using the title Function

To add a title to a graph at the MATLAB command prompt or from an M-file, use the title function.

For example, the following code adds a title to the current axes and sets the value of the FontWeight property to bold.

title('Lotka-Volterra Predator-Prey Population Model',... 
  'FontWeight','bold')

The following figure shows a plot with this title. It also contains a legend and text objects.

Titles are associated with axes. This means that when you make subplots, each axes can have a distinct title. If you create a title in this way when no figures exist, a blank figure with an axes is generated displaying the title you specified.

To edit a title from the MATLAB command prompt or from an M-file, use the set function with the title's handle, as follows:

title_handle = title('This is the original title');
set(title_handle,'String','This is a revised title')

You can also change title strings in plot edit mode or with the Property Inspector. For more information, including code that generates the figure shown here, see the example in Creating Text Annotations with the text or gtext Function.

  


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