Page 158 - Basics of MATLAB and Beyond
P. 158
34.7 Fast Drawing
For fast drawing of graphics (e.g., when animating), consider the follow-
ing extract from the Mathworks’ web site:
Draw movable or changing objects with the EraseMode
property set to xor or background. This prevents re-
rendering the axes when changing these objects. EraseMode
is a property of all objects that are children of axes (line,
text, surface, image, patch).
Set DrawMode (an axes property) to fast. This prevents
matlab from sorting three-dimensional objects, which can
speed things up significantly. The side-effect is that three-
dimensional surface plots will not be drawn properly in this
mode.
Set BackingStore (a figure property) to off. This should
give roughly a factor of two speed-up in normal drawing but
turns off the instantaneous update that normally occurs when
windows are uncovered.
Set NextPlot (a figure property) to new when creating
a GUI. That way when you make plots from the command
window they do not appear in the axes of the GUI’s figure
window.
Wherever possible, recycle figure windows by using the
visible property of figures instead of creating and destroy-
ing them. When done with the window, set visible to
off; when you need the window again, make any changes
to the window and then set visible to on. Creating fig-
ure windows involves much more overhead than setting their
visible property to on.
There is also a property of Figures called doublebuffer. Setting this
property to ’on’ can reduce the flicker when redrawing a figure.
When redrawing a figure from within a loop, matlab will wait until
the final run through the loop before rendering the graphic. If you want
to see intermediate results you need to force matlab to dedraw the
graphic at that point. Use the drawnow command to do this forcing. In
the following example, animation is used to demonstrate the sampling
problem known as aliasing. 16
clf
set(gcf,’doublebuffer’,’on’)
h = plot(0,0,’.’);
for i = 1:1000
t = linspace(0,2*pi,i);
16 This example due to C. Moler, comp.soft-sys.matlab newsgroup.
c 2000 by CRC Press LLC