WR Home      Topic Home      Chapter:  1  2 
<Previous Next>

Chapter 02

Basics of Animation and Forward Kinematics

Page 8

Animating plots in MATLAB®
Basics of Animation: A growing plant example

This simple example shows how Animation can be done in MATLAB® by Updating Plot Object Axis and Color Data. When you create a graph the MATLAB figure stores copies of Data it needs to define x, y and z. Color values it depicts in the plot object itself (e.g., lineseries, barseries, surfaceplot, etc.) If the variables that these values represent are removed or changed, the copies of them in plot object are unaffected. However, you can update these copies (the properties XData, YData, ZData, and CData) at any time; when you do, the graph changes to reflect the updates.
Growing Plant

A simple MATLAB® code is provided below.
main_002.m is to be executed.

This code is easy to digest, please do not proceed further till you execute and understand this code.

Click here to download the code
Click here to view the code.

It is highly recommended to study two more animation examples on "Ways to Animate Plots" from MATLAB® Help. Please refer MATLAB® help. There are two example codes, both show how to animate an area graph of the Pythagorean theorem. The examples compares two different methods of animation.


Using refreshdata to animate an area graph:

c = -pi:.04:pi;
cx = cos(c);
cy = -sin(c);
hf = figure('color','white');
axis off, axis equal
line(cx, cy, 'color', [.4 .4 .8],'LineWidth',3);
title('See Pythagoras Run!','Color',[.6 0 0])
hold on
x = [-1 0 1 -1];
y =   [0 0 0 0];
ht = area(x,y,'facecolor',[.6 0 0])
set(ht,'XDataSource','x')
set(ht,'YDataSource','y')
for j = 1:length(c)
    x(2) = cx(j);
    y(2) = cy(j);

          refreshdata(hf,'caller')
drawnow
end
	
Same animation without using refreshdata:

c = -pi:.04:pi;
cx = cos(c);
cy = -sin(c);
figure('color','white');
axis off, axis equal
line(cx, cy, 'color', [.4 .4 .8],'LineWidth',3);
title('See Pythagoras run!','Color',[.6 0 0])
hold on
x = [-1 0 1 -1];
y =   [0 0 0 0];
ht = area(x,y,'facecolor',[.6 0 0]);
for j = 1:length(c)
    x(2) = cx(j);
    y(2) = cy(j);

          set(ht,'XData',x)
          set(ht,'YData',y)
drawnow
end
	
	
Output:
Ways to Animate Plot
Which method is faster and Why? (Please refer MATLAB® help)



WR Home      Topic Home      Chapter:  1  2 
<Previous Next>