MATLAB Programming/Handle/Figure Handle

Figure handles


A figure is essentially a window that acts as a container for one or more plots, uicontrols, and so on. It is the second highest level object you can create

To create a figure, use the figure function like so:

>> fhandle = figure;

This creates a new figure window and stores all the figure data in the variable fhandle. You can also tell MATLAB to give the figure any number of properties at the same time as it's created using the syntax:

>> fhandle = figure('Propertyname1', value1, 'Propertyname2', value2, ...);

See the documentation "figure properties" page for a list of valid property names for figures.

You can close a figure and destroy its handle programmatically by using the close function:

>> close(fhandle);

If you close the figure, either by using the close function OR by hitting the 'X', you can no longer access the data, and attempts to do so will result in an error. Closing a figure also destroys all the handles to the axes and annotations that depended on it.

The get and set functions


MATLAB allows you to get any property of a figure handle (or actually any type of graphics handle, including axes, line objects, text objects, and so on) by using the 'get' function in the following manner:

>> h = figure;
>> propvar = get(h, 'Propertyname');

will store the property with name 'Propertyname' in the variable propvar. With the 'get' function you can only get one property at a time.

The set function allows you to set any number of properties of figures and other graphics handles. You can change as many properties as you want by calling the set function like this:

>> set(h, 'Propname1', propval1, 'Propname2', propval2, ...);

You can also modify the same properties to the same values for any number of handles, if you simply create an array of handles first:

>> handlevec(1) = figure;
>> handlevec(2) = figure;
>> set(handlevec, 'Name', 'Figure window')

will create two new figures and give them both the same title: 'Figure window'.

See the documentation for valid property names and values for figure properties.

Even if you don't assign a figure to a variable, you can still access it by using the internal MATLAB variable gcf. This is mostly for convenience, so you don't have to pass the figure handle from one function to another to make modifications. However, due to the warning below, you must be VERY careful in use of gcf so that you make sure you are modifying the correct figure in the case of multiple figure programs:

To get a list of the current properties of the current figure, use the syntax:

>> get(gcf)

You can also get or set a specific property by using:

>> get(gcf, 'propertyname');
>> set(gcf, 'propertyname', value);

See the documentation "figure property" page for valid figure property names and the format for their values.

Saving the contents of a figure


To save the entire contents of a figure, use the saveas function:

>> saveas(fhandle, 'X.fig');

will save the contents of the figure with handle fhandle as X.fig. .fig files can be opened and edited manually later if desired, although it is often difficult to open them and re-edit them programmatically in a consistent manner.

You can also use the saveas function to save image formats such as .jpg, .tif, .bmp, .png, and so on. The saveas function is essentially a command line version of the "file --> save as..." option in the figure window itself, but there are some possible resolution differences. Using either of these two methods of saving a figure to a file, the quality of the resulting image is often insufficient for purposes such as inclusion in publications.

In the event that a high quality version of the figure is necessary, it is possible to use MATLAB's print function to print the figure to a vector format (such as Adobe Illustrator's format, obtained with the -dill flag) or to a raster format with a specified resolution (for example, using the two flags -dpng and -r300 would tell MATLAB to print the figure to a PNG file at 300dpi). See the documentation for that function here for more details on the supported output formats.

As an alternative to MATLAB's print function, SVG files can be obtained from many figures (not just simulink models) using the plot2svg function from MATLAB's contributor central (available here). SVG files can be edited using freeware tools such as Inkscape, and also can be opened in Illustrator.