MATLAB For Dummies
Book image
Explore Book Buy On Amazon

You can add animation using MATLAB. The static image playback approach, also called a movie, requires that you grab a series of screenshots of your data as it changes by calling getframe().

Most of the examples of using getframe() show it being used to grab the default object, which are the axes. However, you can supply a handle to any object and make it the focus of your movie. In addition, you can specify that only part of the target object appear in the movie by specifying a cropping rectangle using one of the arguments.

This feature lets you do things like create wipe effects, where you focus in on one object and progressively reveal the rest of the plot from there by changing the rectangular settings.

The frames are placed in a matrix. After you have collected enough frames, you can play your movie using the movie() function. This function accepts a number of inputs, but the three most common are the matrix holding the data to play, the number of times you want to play the movie, and the rate at which to play the movie (the frames per section, or fps).

Here is a typical use of the getframe() and movie() functions.

YSource = [1, 2, 5; 2, 4, 8; 7, 9, 10];
Bar1 = bar3(YSource);
rotate(Bar1, [0, 0, 1], 270)
FigHandle = gcf();
for Frame = 1:32
 Frames(Frame) = getframe(FigHandle,...
  [0, 0, 15 * Frame, 15 * Frame]);
end
clf
movie(FigHandle, Frames, 1, 5);

The code begins by creating a 3D bar chart and rotating it so that you can easily see the bars. The rotate() function accepts three arguments in this case: the handle of the bar chart; a vector containing indicators of which axis to turn (x, y, and z); and the amount to rotate the bar chart in degrees. In this case, the plot rotates around the z-axis.

You can add a call to rotate3d(‘on’) to allow mouse-based rotation of the figure by the user. When you no longer want to allow rotation, call rotate3d(‘off’) instead. Calling rotate3d() by itself toggles between the on and off state. When you supply a handle to the rotate3d() function, the changes affect the figure pointed to by the handle rather than the current figure.

The next step is to generate the movie data. The data consists of 32 frames of data. Each loop obtains information from the figure as a whole, starting in the lower-left corner of the figure. The width and height of the screenshot increases with each loop, so each screenshot is a little bigger and shows a little more of the image as a whole.

After creating the movie, the code clears the screen and then calls movie() to display the movie onscreen. The screenshots are grabbed using the figure handle, not the axis handle, so the movie must also be played using the figure handle.

This is the first argument to movie(). The next argument is the movie matrix itself. The final two arguments determine the number of times to play the movie (once) and the frame rate to use (5 fps).

About This Article

This article is from the book:

About the book authors:

John Paul Mueller is an author and technical editor with experience in application development, database management, machine learning, and deep learning. He has written hundreds of books and articles helping everyday people learn everything from networking to database management.

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: