Draw a particle trajectory diagram in MATLAB and save it as gif

Source: Internet
Author: User
Tags sin

Work needs to draw a particle trajectory in MATLAB and save it as a GIF for presentation.

The plot of the particle trajectories can be used with the comet and Comet3 commands, using the following example:

t = 0:.01:2*pi;
x = cos (2*t). * (cos (t). ^2);
y = sin (2*t). * (Sin (t). ^2);
z = t;
Comet (x,y,0.1); % draw two-dimensional
%comet3 (x,y,z,0.1); % draw three-dimensional

One problem with this is that comet or comet3 cannot control the speed of the animation being drawn, and in order to achieve this, one possible way is to rewrite Comet and Comet3, adding in each for loop:

Pause (interval)

Where interval can be the time interval for each frame entered by the user.

In order to achieve one side of the animated diagram, and save it as GIF animation, my implementation is to rewrite Comet and Comet3, as follows:

Add at the end of each for loop:

frame = GetFrame (GCF);
im = Frame2im (frame);
[IMIND,CM] = Rgb2ind (im,256);
if gifimcount = = 1;
Imwrite (imind,cm,filename, ' gif ', ' delaytime ', interval, ' Loopcount ', INF);
Else
Imwrite (imind,cm,filename, ' gif ', ' delaytime ', interval, ' Writemode ', ' append ');
End
Gifimcount = Gifimcount + 1;

Where Gifimcount is initially set to 0,filename as the name of the GIF file to be stored. The Drawnow statement in the original for loop can be removed because the getframe itself calls the Drawnow function.

However, there is a problem, that is, the only way to get the animation of the current frame, the wake disappeared, that is, unable to get a continuous animation effect. This question I originally wanted to be sure is getframe inside somewhere to erase the graph, can modify the getframe to solve, but still did not solve. Finally find a workaround, that is, to rewrite the comet and COMET3 functions, do not carry out the original body and tail of the separate drawing, Comet and Comet3 in the "% Grow the body" and "% clean up the tail" two parts removed, only left "% Primary Loop "section, and slightly make the following modifications:

% Primary Loop
For i = 1:m
Set (head, ' XData ', x (i), ' Ydata ', y (i))
Set (tail, ' XData ', X (1:i), ' Ydata ', Y (1:i))
frame = GetFrame (GCF);
im = Frame2im (frame);
[IMIND,CM] = Rgb2ind (im,256);
if gifimcount = = 1;
Imwrite (imind,cm,filename, ' gif ', ' delaytime ', interval, ' Loopcount ', INF);
Else
Imwrite (imind,cm,filename, ' gif ', ' delaytime ', interval, ' Writemode ', ' append ');
End
Gifimcount = Gifimcount + 1;
End

The effect is that each drawing redraws the point from 1 to I. Through the above modification, it is possible to achieve the effect of plotting the particle trajectory in MATLAB and saving it as GIF.

Here are the effects:

If you have a better way, please let me know, thank you!

Draw a particle trajectory diagram in MATLAB and save it as gif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.