Tuesday, January 22, 2013

Animation in Matlab


http://www.maelabs.ucsd.edu/mae150/mae150_resources/Matlab_Tips/intro_to_kinematic_plotting.htm

%--------------- Introduction to Kinematic Plotting ---------------
%
% MATLAB provides three ways of generating animations:

% 1. Repeatedly execute the 'plot' command in a loop. This is the easiest method
%    but results in a flashing screen.  (see the first example)
%
% 2. Set up a graph using handle graphics, and repeatedly execute the 'drawnow' command.
%    This eliminates the flashing problem. (see the second example)

% 3. Save a number of different pictures and then play them back as a movie. This is useful for very complex figures that would be slow to animate using the prior methods. (see Matlab help)

% Simple (but effective) visualizations can be done with line plots. That is, draw a new line in a new location after a short pause and erase the old line.
% (to plot 3D shapes see the patch command)

% Matlab can help visualize and animate the kinematics of mechanisms and other objects

As you can see, this code allows you to create a simple animation.

%------------ A simple animation using 'plot' and 'drawnow' ------------

% A Simple Animation of a rotation arm

theta_step = 5*pi/180;
r = 1;               % radius of the arm

for theta=0:theta_step:2*pi

% In order to draw a stick figure you need to have a start point and an end point
% (in the case of a closed figure the end point must end at the start point;
% this allows the figure to be completely closed), the following two commands define
% the points of the stick figure:
    data_pt(1,:) = [0 0];                        % first point is the origin
    data_pt(2,:) = [r*cos(theta) r*sin(theta)];  % second point is end of vector
    plot(data_pt(:,1),data_pt(:,2));             % connect the points with a line
    axis([-1.5 1.5 -1.5 1.5]);   % set the axis limit to include the range of animation
    axis manual     % freezes the axis scaling
    pause(0.1)      % puts a delay in seconds (if time is zero, a keyboard hit is required)
end

disp('Hit the Space Bar to Continue');
pause;

%********************* Ways to reduce flashing ***********************

%  Do the animation again while reduce flashing.
%  There are two steps to reduce the flashing on the plot. Please see
%  ‘Introduction to Graphic Handles’ for help on graphic handles.

%  1. define EraseMode in the ‘plot’ command. 
%  2. instead of using ‘plot’ repeatedly, change the data by ‘set’ command.

theta_step = 5*pi/180;
theta = 0:theta_step:2*pi;          % generate and store the angle sequence
x_pt = [0 1];                       % define the end points of the stick figure
y_pt=[0 0];             
axis([-1.5 1.5 -1.5 1.5]);
hold on;
hndl=plot(x_pt,y_pt,'EraseMode','background'); % connect the end points,
                                               % and use "EraseMode" to REDUCE FLASHING

set(gca,'DataAspectRatio',[1 1 1]);            % make x,y-axis relative scaling equal

for j=1:length(theta)
    x_pt=[0  cos(theta(j))];
    y_pt=[0  sin(theta(j))];
    set(hndl,'XData',x_pt,'YData',y_pt);       % REDUCE FLASHING
    drawnow         % forces Matlab to redraw the plot at the current values
    pause(.1)
end

%  Compare this animation with the previous one: 
%  In this case, we generate and store the end points BEFORE we call them in each loop.
%  In the previous one, we generate the end points and draw a new line IN each loop.
%  When the process of generating data is complex and the computational time delay
%  cannot be ignored, we usually do the computation first, then draw the data in each loop

%------------------------------------------------------------
% Key functions:  drawnow, pause(time), set
% Key properties: EraseMode, DataAspectRatio, XData, YData


1 comment:

  1. If you put
    title(num2str(j))
    in the loop, the traces will not be maintained

    ReplyDelete