MATLAB provides very powerful techniques to visualize data. In this chapter, we look at how to visualize data and create animations. Visualizing data was already touched on in Chapter 1. Therefore, the animation techniques are emphasized in this chapter. Animations can be created by using three basic techniques in MATLAB. These methods can be summarized as updating coordinates, applying transformation to objects, and creating movies.
Data Visualization
Some Functions Related to 3-D Plotting
Function | Description |
---|---|
plot3(x,y,z) | Creates a 3-D line plot |
bar3 | Plots 3-D bar graphs |
comet3(x,y,z) | Plots an animated 3-D graph |
ezmesh | Visualizes the function in 3-D |
ezplot3 | A 3-D parametric curve plotter |
mesh(x,y,z) | Creates a meshed-surface plot |
pie3 | Draws a 3-D pie chart |
scatter3 | Is a 3-D scatter plot function |
stem3 | Creates a 3-D stem plot |
surf | Plots a 3-D shaded surface |
waterfall | Creates a waterfall plot |
Example 7-1. Consider the three-dimensional function given here.
0 ≤ t ≤ 4π, x = sin (t), y = cos (t), z = t.
Plot the graph of x, y, and z in a single figure.
Solution 7-1. The following code can be used to accomplish the given task.
There are two important functions that produce values with two variables in MATLAB, namely, peaks and meshgrid.
The function peaks(x,y) produces a 49 × 49 matrix by default via Gaussian distribution. The function meshgrid(x,y) replicates the grid vectors x and y to produce a full grid.
Using the functions [X,Y,Z] = peaks(x,y,z) and [X,Y,Z] = meshgrid(x,y,z), it is possible to create three variables, as well.
where x and y are between -1.5 and 1.5.
Solution 7-2. The following code can be used to accomplish the given task.
Animation
Three techniques can be applied to create an animation with data: updating coordinates, applying transformation, and creating movies.
Updating Coordinates
In this type of animation, the object properties are updated and called within a loop. Most of the time these properties are the data of x and y coordinates as illustrated in Example 7-3.
Write code to animate the function y by changing its coefficient.
Solution 7-3. The following code can be used to accomplish the given task.
In the preceding code, the function y is plotted with coefficients starting from to 1. After each plotting in the loop, the pause(0.02) command pauses the computer for 0.02 seconds to see the changes in each frame visually.
Example 7-4. Write code that uses the drawnow function to show the animation. The code should draw lines in a circle.
Solution 7-4. The following code can be used to accomplish the given task.
Applying Transformation
In this technique, transformation is applied to objects. The function hgtransform is used to create the transform object.
Example 7-5. Write code to animate a 3-D star around the z-axis while the star is scaled from a large size to a smaller size by using the hgtransform .
Solution 7-5. The following code can be used to accomplish the given task.
Creating Movies
Creating a movie is another method to animate the data. In this method, the picture of each drawing in each iteration is obtained using the getframe function . Then, using the movie function , the animation is created.
write code to animate the function y as a movie.
Solution 7-6. The following code can be used to accomplish the given task.
When you want to save the movie that you created, use the functions writeVideo and VideoWriter.
Example 7-7. Create a movie by using the functions peaks and surf. Then, save the movie as an *.avi file.
Solution 7-7. The following code can be used to create the *.avi file .
In this code, the function VideoWriter creates a video writer object. The class of this object is the same as its name, VideoWriter. Using the function writeVideo, the frames are written into that file. Once the code is executed, a file named myfile.avi is created and saved into your current directory.
Problems
7.1. Consider the three-dimensional function given here.
Plot the graph of x, y, and z in a single figure.
7.2. Consider the following function.
Write code to animate the function y by changing its coefficient.
7.3. Write code that uses the drawnow function to show the animation. The code should draw lines in a square.
7.4. Write a code to animate a 3-D star around the y-axis while the star is scaled from a small size to a larger size using the hgtransform function.
7.5. For the following function given by
write code to animate the function y twice as a movie and save the created animation.