MATLAB For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon

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.

  2. Type format compact and press Enter.

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

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

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

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

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

  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.

  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.

About This Article

This article is from the book:

About the book authors:

Jim Sizemore is professor of physics and engineering at Tyler Junior College. For over 25 years, Sizemore has worked in the semiconductor and software industries as a process engineer, device physicist, and software developer. John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++.

This article can be found in the category: