Page 96 - A Guide to MATLAB for Beginners and Experienced Users
P. 96

Special Effects       77


           Animations
                     The simplest way to produce an animated picture is with comet, which pro-
                     duces a parametric plot of a curve (the way plot does), except that you can
                     see the curve being traced out in time. For example,


                       >> t = 0:0.01*pi:2*pi;
                       >> figure; axis equal; axis([-1 1 -1 1]); hold on
                       >> comet(cos(t), sin(t))


                     displays uniform circular motion.
                       For more complicated animations, you can use getframe and movie.The
                     command getframe captures the active figure window for one frame of the
                     movie, and movie then plays back the result. For example, the following (in
                     MATLAB 5.3 or later — earlier versions of the software used a slightly differ-
                     ent syntax) produces a movie of a vibrating string:


                       >> x = 0:0.01:1;
                       >> for j = 0:50
                            plot(x, sin(j*pi/5)*sin(pi*x)), axis([0, 1, -2, 2])
                            M(j+1) = getframe;
                       end
                       >> movie(M)


                     It is worth noting that the axis command here is important, to ensure that
                     each frame of the movie is drawn with the same coordinate axes. (Other-
                     wise the scale of the axes will be different in each frame and the result-
                     ing movie will be totally misleading.) The semicolon after the getframe
                     command is also important; it prevents the spewing forth of a lot of nu-
                     merical data with each frame of the movie. Finally, make sure that while
                     MATLAB executes the loop that generates the frames, you do not cover the
                     active figure window withanother window (suchas the Command Window).
                     If you do, the contents of the other window will be stored in the frames of the
                     movie.


                     MATLAB 6 has a new command movieview that you can use in place of
                       movie to view the animation in a separate window, with a button to replay
                       the movie when it is done.
   91   92   93   94   95   96   97   98   99   100   101