MATLAB:Animation

From PrattWiki
Jump to navigation Jump to search

MATLAB plots can be animated in a variety of ways. Generally, this involves making a plot, making sure MATLAB draws it, then saving the plot to a movie frame.

Example

The following example will first plot a black curve that winds around a torus. It will then animate a blue dot following the path.

clear; format short e
figure(1); clf

t = linspace(0, 2*pi, 1000)
x = @(t) cos(t) .* (1+.4*cos(30*t));
y = @(t) sin(t) .* (1+.4*cos(30*t));
z = @(t) 0.4*sin(30*t)

plot3(x(t), y(t), z(t), 'k-')
axis equal

hold on
MyDot = plot3(x(t(1)), y(t(1)), z(t(1)), 'bo', 'MarkerFaceColor', 'b')
hold off
M(1) = getframe

for k=2:length(t)
    set(MyDot, 'XData', x(t(k)), 'YData', y(t(k)), 'ZData', z(t(k)))
    drawnow
end


Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References