0

so I've created a simple planet simulation but because my plot is inside my for loop it will close and open (super fast) a new plot creating the sensation of flickering of my orbiting planets.

Here's a part of my for loop:

for step=1:n




    %% Mercury

    r_vekt_mercury=-p_mercury;
    r_mercury=sqrt(x_mercury^2 + y_mercury^2); %Radius in AU
    a_mercury=((G*m_0)/((r_mercury)^3))*r_vekt_mercury;
    v_mercury=a_mercury*dt+v_mercury;
    p_mercury=v_mercury*dt+p_mercury;

    plot(psol(1),psol(2),'y.','markersize',60); %% Drawing ze sun
    drawnow;
    xlabel('AU');
    ylabel('AU');
    hold on

    plot(p_mercury(1),p_mercury(2),'g.','markersize',25);
    axis([-1 1 -1 1]); %AU
    drawnow;
    hold on

end

So as you see this for loop will create a new plot whenever it goes through the loop which creates the flickering.. Any solution?

Rida
  • 13
  • 2
  • The short answer: make a plot, capture the graphics handle, and use the `set` command to update. See the linked duplicate. – gnovice Dec 22 '16 at 03:30
  • Here's another related one: [Real time plot in matlab](http://stackoverflow.com/q/3115833/52738) – gnovice Dec 22 '16 at 03:34
  • Thanks for the help! I looked through both links and I couldn't actually figure out how to use it for my example. Any chance you could help me out? – Rida Dec 22 '16 at 05:04
  • It's discussed in a few of the examples. You would have a line before your loop like `h_mercury = plot(0, 0, 'g.', 'markersize', 25);`, which will create a plot object (i.e. one point), then in your loop you would replace the plot line with `set(h_mercury, 'XData', p_mercury(1), 'YData', p_mercury(2));`, which will update just your plotted point instead of remaking the whole plot. – gnovice Dec 22 '16 at 05:34
  • I did indeed try to figure out the examples (Trying to use the third method) but I couldn't figure it out! Anyway I got it working using the exact code you wrote BUT it doesn't work for multiple points! I.e. I use the exact same code but replace the h_mercury with h_earth and the x and y variables with the respective ones for the earth. I do not change XDATA and YDATA though! Also, dumb question, but the " 'XData' " is that a part of the code or something I have to fill in? And if this was 3D would I just add " 'ZData', p_mercury(3) "? – Rida Dec 22 '16 at 15:50
  • If you want to add to a plot, you have to call `hold on` after the first call to plot. "XData" is part of the code (it's the property you're setting). And you are correct about using "ZData". – gnovice Dec 22 '16 at 15:57

0 Answers0