Task: Write a script to create a movie showing the motion of a simple wave of the form h=sin(2*pi*(x/L - t/T)). Let x range from 0 to 30 and t range from 0 to 50. Let L=15 and T=10.
A movie is created by making a series of changing figures each of which is saved with the getframe command. Consider the following simple routine:
x=0:.1:10; NumFrames=100; figure for it=1:NumFrames y=sin(2*pi*(x-.2*it)).*sin(2*pi*(.1*x-.006*it)); plot(x,y) title('Modulated Sine Wave') axis([0 10 -1 1]) % control the plot size to be consisent between frames m(it)=getframe; end
You can display the newly created movie with the command movie(m,loops,FrameSpeed), where the first argument is the variable holding the movie (m in this case), the second argument (loops) is the number of times to loop through the movie, and the third argument (FrameSpeed) is how fast (frames per second, or fps) to show the figures. A reasonable choice is 12 fps. To avoid difficulties, it is recommended that you issue the command cla, which clears the current axes, before showing the movie.
Recall that the view command changes the viewpoint of a surf image. A fly-around movie can be made by displaying information from a 2D array with surf and then changing the view and saving the frame. This lets you see all sides of an image in a movie.
A number of tools exist to create multiple related images in a single file. It is possible to convert this set of images into a matlab movie with immovie. Alternatively, you can animate multiple images with implay. Look at help for details on using these tools.
A common format for movies is avi which has more information about the movie and can include sound along with images. It is possible to create an avi movie from your matlab movie with the command movie2avi. As you might expect, you will need to provide additional details.
You can create directly an avi movie. Open the file with avifile. Make a figure or display and image and save it to your movie with addframe. At the end of the process, close the movie file with close.
There are tools to get information about an existing avi movie. aviinfo will extract the details. aviread will read an existing movie into matlab.
Flowchart to accomplish task:
%%% set up x array %%% set number of frames %%% set length and time parameters %%% loop through frames %%% create y array for this time %%% plot line %%% add title %%% control axis size %%% save frame to movie array %%% display movie
Script to accomplish task:
%%% set up x array x=0:.1:10; %%% set number of frames NumFrames=51; %%% set length and time parameters T=10;L=15; figure %%% loop through frames for it=1:NumFrames %%% create h array for this time time=it-1; h=sin(2*pi*(x/L-time/T)); %%% plot line plot(x,h) %%% add title title('Travelling Sine Wave') %%% control axis size axis([0 10 -1 1]) %%% save frame to movie array m(it)=getframe; end cla; % clear current axis variables %%% display movie movie(m,2,10);