2.5 Animation

Animation is not the same thing as playing a movie. movie is a command which plays pre-recorded information. Animation is the process of continuously drawing and/or changing an object. Animation is used in the Lorenz Attractor demo. In case you are not familiar with this demo, it shows an object that is continuously moving.

The trick behind animation in MATLAB is the EraseMode property. The EraseMode property controls how and when the object is drawn and deleted. By default, when an object is added or changed, new objects or changes are not drawn until the axes are refreshed. This occurs instantly from the command line; however, in M-files, the axes are not updated until a drawnow, getframe, pause, or a return to the prompt. When the axes are updated, every object is also updated. This means that they are redrawn. Not all objects have an EraseMode property, only lines, patches, surfaces, and text.

As mentioned above, the EraseMode property controls how and when an object is drawn and deleted. There are four possible settings for EraseMode. They are:

normal
background
xor
none
When EraseMode is set to normal, the default, the axes control when the object is updated. This means that the axes will flicker when an object is drawn, changed, or deleted.

When EraseMode is set to background, the object deletes itself by drawing itself in the Figure's background color. The major disadvantage with this method is that objects behind this object are damaged.

When EraseMode is set to xor, the object is drawn and erased by xor'ing it with the color beneath it. When the object is erased, objects behind it are not damaged.

When EraseMode is set to none, the object is not erased when it is moved or destroyed.

When are these different modes useful? If you want to eliminate flickering, you should not use normal. If you want to see a trail left behind, as in the Lorenz Attractor demo, then use none. If you are concerned about the color of the object and do not care what happens to objects beneath it, then use background. If you do not want to damage objects that are beneath the object, then use xor.

As stated above, the benefit of using EraseMode is that you can control how and when objects are drawn and deleted. By using any setting other than normal, you can greatly increase the speed of the animation. Try the following two programs to verify this:

% Program 1:  EraseMode set to normal
h = plot(1:100,.01:.01:1)
axis([0 100 0 1])
tic
for x = 1:100
  set(h,'YData',rand(1,100))
  drawnow
end
toc
% Program 2: 'EraseMode' set to xor
h = plot(1:100,.01:.01:1,'EraseMode','xor')
axis([0 100 0 1])
tic
for x = 1:100
  set(h,'YData',rand(1,100))
  % drawnow  % drawnow is need on the under MS-Windows
end
toc
Just as a point of reference, on a 486DX2-66V, it took 23.01 seconds to run the first program and 3.51 seconds to run the second.

As you can see, the setting of EraseMode can greatly influence the speed of the program. When the EraseMode is set to something other then normal, the axes and other objects are not redrawn. This saves a lot of time and makes the animation run smoothly.

In addition to animation, the EraseMode property can be used to add lines, patches, surfaces, and text to plots without causing the plot to redraw. For example:

plot(1:10)
text(1,1,'Flickers')
text(5,5,'No Flicker','EraseMode','Xor')
The first text object will cause the screen to update, the second will not.

One last note: Another property which influences animation is BackingStore. BackingStore is a property of figure and is used to fill in areas quickly that were covered by other windows. When BackingStore is set to On, the default, two copies of each figure are made. One is displayed in the Figure Window, and the other is used to patch areas that were previously covered. By setting BackingStore to Off, only one copy is made, thus reducing the amount of work required to create the plot.

The advantage of setting the BackingStore property to On is that the entire Figure Window does not have to be updated to fill in a region that was covered by another window. The disadvantage is that setting BackingStore to On forces MATLAB to use additional resources.

The advantage of setting the BackingStore property to Off is that it reduces the resources required to produce a plot. The major disadvantage is that the entire Figure Window must be refreshed to fill in areas that were covered by other windows.

(c) Copyright 1994 by The MathWorks, Inc.