Getting Started with MATLAB   Search    Help Desk 

Handle Graphics

MATLAB provides a set of low-level functions that allows you to create and manipulate lines, surfaces, and other graphics objects. This system is called Handle Graphics®.

Graphics Objects

Graphics objects are the basic drawing primitives of MATLAB's Handle Graphics system. The objects are organized in a tree structured hierarchy. This reflects the interdependence of the graphics objects. For example, Line objects require Axes objects as a frame of reference. In turn, Axes objects exist only within Figure objects.

Graphics Objects

There are eleven kinds of Handle Graphics objects:

Object Handles

Every individual graphics object has a unique identifier, called a handle, that MATLAB assigns to the object when it is created. Some graphs, such as multiple line plots, are composed of multiple objects, each of which has its own handle. Rather than attempting to read handles off the screen and retype them, you will find that it is always better to store the value in a variable and pass that variable whenever a handle is required.

The handle of the root object is always zero. The handle of a figure is an integer that, by default, is displayed in the window title. Other object handles are floating-point numbers that contain information used by MATLAB. For example, if A is the Dürer magic square, then

creates a line plot with four lines, one for each column of A. It also returns a vector of handles such as

The actual numerical values are irrelevant and may vary from system to system. Whatever the numbers are, the important fact is that h(1) is the handle for the first line in the plot, h(2) is the handle for the second, and so on.

MATLAB provides several functions to access frequently used object handles:

You can use these functions as input arguments to other functions that require figure and axes handles. Obtain the handle of other objects you create at the time of creation. All MATLAB functions that create objects return the handle (or a vector of handles) of the object created.

Remove an object using the delete function, passing the object's handle as an argument. For example, delete the current axes (and all of its children) with the statement:

Object Creation Functions

Calling the function named after any object creates one of those objects. For example, the text function creates text objects, the figure function creates figure objects, and so on. MATLAB's high-level graphics functions (like plot and surf) call the appropriate low-level function to draw their respective graphics.

Low-level functions simply create one of the eleven graphics objects defined by MATLAB with the exception of the root object, which only MATLAB can create. For example:

Object Properties

All objects have properties that control how they are displayed. MATLAB provides two mechanisms for setting the values of properties. Object properties can be set by the object creation function, or can be changed with the set function after the object already exists. For example, these statements create three objects and override some of their default properties.



days is a character array containing abbreviations for the days of the week and temp is a numeric array of typical temperatures. The figure window is created by calling figure with no arguments, so it has the default properties. The axes exists within the figure and has a specified range for the scaling of the y-axis and specified labels for the tick marks on the x-axis. The line exists within the axes and has specified values for the x and y data. The three object handles f, a, and h, are saved for later use.

set and get

Object properties are specified by referencing the object after its creation. To do this, use the handle returned by the creating function.

The set function allows you to set any object's property by specifying the object's handle and any number of property name/property value pairs. For instance, to change the color and width of the line from the previous example,

To see a list of all settable properties for a particular object, call set with the object's handle:

To see a list of all current settings properties for a particular object, call get with the object's handles:

To query the value of a property, use get with the property name:

The axes object carries many of the detailed properties of the overall graphic. For example, the title is another child of the axes. The statements:

specify a particular title. The title function provides another interface to the same properties.

Graphics User Interfaces

Here is a simple example illustrating how to use Handle Graphics to build user interfaces. The statement

creates a pushbutton in the center of a figure window and returns a handle to the new object. But, so far, clicking on the button does nothing. The statement

creates a string containing a command that alters the pushbutton's position. Repeated execution of

moves the button to random positions. Finally,

installs s as the button's callback action, so every time you click on the button, it moves to a new position.

Animations

MATLAB provides several ways of generating moving, animated, graphics. Using the EraseMode property is appropriate for long sequences of simple plots where the change from frame to frame is minimal. Here is an example showing simulated Brownian motion. Specify a number of points, like

and a temperature or velocity, such as

The best values for these two parameters depend upon the speed of your particular computer. Generate n random points with (x,y) coordinates between -1/2 and +1/2

Plot the points in a square with sides at -1 and +1. Save the handle for the vector of points and set its EraseMode to xor. This tells the MATLAB graphics system not to redraw the entire plot when the coordinates of one point are changed, but to restore the background color in the vicinity of the point using an "exclusive or" operation.

Now begin the animation. Here is an infinite while loop, which you will eventually break out of by typing <ctrl>-c. Each time through the loop, add a small amount of normally distributed random noise to the coordinates of the points. Then, instead of creating an entirely new plot, simply change the XData and YData properties of the original plot.

How long does it take for one of the points to get outside of the square? How long before all of the points are outside the square?



Movies

If you increase the number of points in the Brownian motion example to something like n = 300, the motion is no longer very fluid; it takes too much time to draw each time step. It becomes more effective to save a predetermined number of frames as bitmaps and to play them back as a movie.

First, decide on the number of frames, say

Next, set up the first plot as before, except do not use EraseMode.

Now, allocate enough memory to save the complete movie,

This sets aside a large matrix with nframes columns. Each column is long enough to save one frame. The total amount of memory required is proportional to the number of frames, and to the area of the current axes; it is independent of the complexity of the particular plot. For 50 frames and the default axes, over 7.5 megabytes of memory is required. This example is using square axes, which is slightly smaller, so only about 6 megabytes is required.

Generate the movie and use getframe to capture each frame.

Finally, play the movie 30 times.



[ Previous | Help Desk | Next ]