MATLAB Programming/Handle Graphics
This book discusses some ways of manipulating graphics in MATLAB.
What is a handle?
All of the graphics capabilities explained thusfar are ways to generate plots without worrying much about the details. However, very often, you will want a plot that looks a very specific way. You can go into the GUI and change all of the settings every time, but this gets very tedious and wastes a lot of time. If you want a way to generate a plot that has the same look all the time, it is time to delve into the realm of handle graphics.
A handle is a floating-point scalar that points to a large list of different properties. Each element of a plot has its own properties, and its own handle. Handles are organized into a tree-structured hierarchy, starting from the handle for your monitor (named 0) and branching down to separate handles for children like axis labels, text annotations, error bars, and anything else that can go on a plot. Handles at all levels can be controlled interactively or adjusted programmatically, by changing the values of the handle properties using specific functions.
Monitor properties: the 0 handle
Monitor properties are useful to know when designing graphic user interfaces. For example, you may want to check that a figure window fits inside the user's screen. In order to get a list of the properties of the screen, type:
To get a specific property, type
>> get(0, 'propertyname');
See the documentation for a complete list of property names and what they mean.
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:
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:
||The current figure is the last one to be modified, not necessarily the one that's open|
To get a list of the current properties of the current figure, use the syntax:
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.
See the documentation for a list of valid axis properties and values of those properties.