Getting Animation Automatically with a comet Plot

By Jim Sizemore, John Paul Mueller

MATLAB provides a few plots that create animated effects. One of the more interesting plots that naturally lends itself to presentations is the comet() plot. When you create a plot using this function, the beginning of the line forms the comet head, while the end of the line forms the tail.

As MATLAB performs the calculation and creates more points, the comet moves along its way until the plot is finished. It’s an interesting effect that helps people see the manner in which the plots are created, which is especially helpful with time-related plots.

The following steps help you create a comet() plot.

  1. Open your copy of MATLAB.

    The MATLAB application starts with the Command window displayed.

    image0.jpg

  2. Type format compact and press Enter.

    MATLAB displays the output of anything you type using the compact format.

    image1.jpg

  3. Type Time = -pi:.01:pi; and press Enter.

    The Time variable contains the individual intervals that MATLAB plots for you.

    image2.jpg

  4. Type Y = sin(Time); and press Enter.

    A new variable is created with the individual instance values needed to plot the diagram.

    image3.jpg

  5. Type comet(Y); and press Enter.

    MATLAB draws the plot. However, notice the animation effect. The plot begins at the left side of the plot area and moves toward the right. The plot doesn’t appear all at one time; instead, you see the plot being drawn.

    image4.jpg

  6. Type comet(Y); and press Enter again.

    Notice how MATLAB clears the display area and redraws the plot from scratch. The animation can be repeated in a loop for effect.

  7. Close your copy of MATLAB.