Simple, infinite Animation (or pause on keypress)

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

Réponses (1)

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 commentaires

mattberg1
mattberg1 le 28 Nov 2011
Hi,
alright, thanks, I got it working, but I still get an Error I don't understand:
??? Undefined function or method 'stopfcn' for input arguments of type 'double'.
??? Error using ==> delete
Error while evaluating figure DeleteFcn
??? Error using ==> set
Invalid handle object.
Any Idea on how to get rid of them?
Greetings, Matt
David Young
David Young le 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
mattberg1 le 28 Nov 2011
I don't know which version is installed in the CIPPools here, but I think it is 2011b.
Anyways, thanks, the problem's solved (it was the missing nesting) and the satellite is orbiting fine.
Thanks for your help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Gravitation, Cosmology & Astrophysics dans Centre d'aide et File Exchange

Question posée :

le 27 Nov 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by