Simple, infinite Animation (or pause on keypress)
Show older comments
Hi there,
I'm currently working on a little program in what I need an Animation (Motion on a planetary). So far I managed to plot a circle (for earth) and the ellipse for the the orbit, but now, how do I get a little moving circle on the ellipse which moves and moves until the axiswindows is closed (or alternatively paused on keypress)? I already got the formulas for the motion, but I have no clue on how to get it moving.
My current code is this (simplified):
plot(earth)
axis equal;
hold on;
Input (excentricity, semimajor axis)
Calculate missing parameters(semiminor axis, etc);
plot(orbit)
axis equal;
hold on;
hold off;
Also, another question, how do I put text out while the infiniteloop plots the motion (velocitiy, current satellite height)?
Greetings, Matt
Answers (1)
David Young
on 27 Nov 2011
Here's a function that may be some use - but others may have neater or more flexible solutions. It doesn't plot the orbit, just the objects, and it does a circle, not an ellipse, so you'll have to put your ellipse calculation in instead, but that should be fairly obvious. The main contribution here is to suggest one way to stop the animation when the window is closed.
function testanimate
cont = true;
plot(0, 0, 'o');
set(gcf, 'DeleteFcn', @stopfcn);
theta = 0;
thetainc = 0.01;
radius = 80;
while cont
plot(0, 0, 'go');
% compute x and y of orbiting object
[x, y] = pol2cart(theta, radius);
hold on;
plot(x, y, 'ro');
hold off;
axis equal;
axis([-100 100 -100 100]);
drawnow;
theta = theta + thetainc;
end
function stopfcn(~,~,~)
cont = false;
end
end
To print the text, put a call to disp or fprintf in the loop.
3 Comments
mattberg1
on 28 Nov 2011
David Young
on 28 Nov 2011
What version of MATLAB are you using? The function above runs fine under 2011b, but I'd expect it to be OK on all recent versions. Note that as it's a function, you do have to save the whole thing in an m-file and call it - you can't execute parts of it at the command line or in cell mode, and you can't remove the first line to make it a script. The stopfcn function must be nested in the outer function.
mattberg1
on 28 Nov 2011
Categories
Find more on Gravitation, Cosmology & Astrophysics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!