In MATLAB, the process of creating a movie animation is divided into the following four steps:
step1: call the moviein function to initialize the memory (this step can be omitted in Matlab5.3 and above), and create a matrix large enough to accommodate the current coordinates based on the A series of specified figures (here called frames) of the axis size.
step2: call the getframe function to generate each frame. This function returns a column vector from which a movie animation matrix can be created.
The getframe function can capture animation frames and save them to a matrix. Generally, this function is placed in a for loop to get a series of animation frames.
The function format is:
(1) F=gefframe, get the animation frame from the current graphics frame
(2) F=gefframe(h), get the animation frame from the graphics handle h
(3) F=getframe(h, rect), Get the animation frame
step3 from the specified area rec of the graphics handle h : call the movie function to run the movie animation according to the specified speed and times.
After creating a series of animation frames, you can use the movie function to play these animation frames.
The main formats of this function are:
(1) movie(M), play the animation frame in matrix M once
(2) movie(M,n), play the animation frame in matrix M n times
(3) movie(M ,n,fps), play the animation frames in the matrix M n times at the speed of fps frames per second
step4: Call the movie2avi function to convert a series of animation frames in the matrix into a video file avi file. In this way, the animation can be played even out of the matlab environment.
For details, see:
The classic format of this method is:
%----------------------------------------------- % Record movie animation for j= 1 :n % % Here we enter our drawing command % M(j) = getframe; end movie(M)
% Give an example of the motion simulation of the cam mechanism that I have done %% The following is only part of the code of the motion movie %-------Motion simulation starts------ ------------- figure( 2 ) m=moviein(20); j=0; for i=1:360 j=j+1; delta(i) =i*hd;% cam angle xy =[xp ' ,yp ' ];% cam actual contour curve coordinates A1 =[cos(delta(i)),sin(delta(i)); % cam curve coordinate rotation matrix - sin(delta(i)),cos(delta(i))]; xy =xy*A1;% rotate the coordinates of the actual cam profile curve clf; %-----------------Plot the cam ------------------ plot(xy(:, 1 ),xy(: , 2 ));% draw cam hold on;grid on;axis equal; axis([(-180) (470) (-200) (240)]); plot([ -(r0+h- 40 ) (r0+h) ],[ 0 0 ], ' k ' , ' LineWidth ' , 2 );% draw the cam horizontal axis plot([ 0 0 ],[-(r0+h) (r0+rr)], ' k ' , ' LineWidth ' , 2 );% draw the vertical axis of the cam plot(r0 *cos(ct),r0*sin(ct), ' g-- ' , ' LineWidth ' , 2 );% draw the base circle plot(e *cos(ct),e*sin(ct), ' c- ' , ' LineWidth ' , 2 );% draw the offset circle plot(e +rr*cos(ct),s0+s(i)+rr*sin(ct), ' k ' , ' LineWidth ' , 2 );% draw a roller circle plot([ee +rr*cos(-phi(i))],[s0+s(i) s0+s(i)+rr*sin(-phi(i))], ' k ' , ' LineWidth ' , 2 ); % draw roller reticle plot([ee],[s0 +s(i) s0+s(i)+ 40 ], ' k ' , ' LineWidth ' , 2 );% draw putter %------------- -------Plot the putter curve---------------------------- plot([ 1 : 360 ]+r0+ h,s+s0);% draw putter curve plot([(r0 +h) (r0+h+ 360 )],[s0,s0], ' k ' , ' LineWidth ' , 2 );% draw the vertical axis of the push rod plot([(r0 +h) (r0+h)],[s0 s0+h], ' k ' , ' LineWidth ' , 2 );% draw the horizontal axis plot(i +r0+h,s(i)+s0, ' r. ' , ' LineWidth ' , 1.5 );% draw the coordinate moving point of the putter curve title( ' Offset linear roller pusher disc cam design ' ); xlabel('x/mm'); ylabel('y/mm'); m(j)=getframe; end movie(m);
% Single frame display method
f = getframe(gcf);
colormap(f.colormap);
image(f.cdata);
%------------------------------------------------
Additionally, using the immovie function, we can create MATALB movie animations from multi-frame image arrays. [Not used]