How to Add Movie Animation in MATLAB - dummies

# How to Add Movie Animation in MATLAB

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).