Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Exporting(Saving) an animated plot
Replies: 4   Last Post: Jan 1, 2014 6:43 PM

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,630 Registered: 2/7/05
Re: Exporting(Saving) an animated plot
Posted: Dec 26, 2013 5:10 PM

On 12/26/2013 3:58 PM, Dila wrote:
> Hi,
>
> I have a code which plots an animated graph of flowing waves.
>
> And my wish is to save it as .gif or .avi, or just export it somehow to include in a presentation.
>
> My code is added below.
>
> Dila.
>
> clc
> clear all
> hold off
>
> k = 2*pi*1;
> w = k*343;
> L = 2.2;
> x = -3:0.001:3;
>
> % Dipole: normalized variables
> p = [exp(-j*k*abs(x)).*sign(x)];
> u = exp(-j*k*abs(x));
>
> t = 0;
>
> figure
> subplot(211)
> hp = plot(x,real(p*exp(j*w*t)));
> title('normalized p(x,t)')
> set(hp,'EraseMode','xor');
> grid
> axis([-3 3 -1 1])
> hold on
> plot([0 0],[-1 1],'b--')
> hold off
>
> subplot(212)
> hu = plot(x,real(u*exp(j*w*t)));
> set(hu,'EraseMode','xor');
> grid
> axis([-3 3 -1 1])
> title('normalized u(x,t)')
> hold on
> plot([0 0],[-1 1],'b--')
> xlabel('CTRL-C to terminate')
> hold off
>
> while 1
> t = t+0.0001;
> set(hp,'YData',real(p*exp(j*w*t)));
> set(hu,'YData',real(u*exp(j*w*t)));
> pause(0.03)
> drawnow
> end
>

There might be an easier way. But when I did this before,
I used this method

-------------------------------
.....
if GENERATE_GIF
f = getframe(gcf);
frame_number = frame_number+1;
im(:,:,1,frame_number)=rgb2ind(f.cdata,map,'nodither');
else
im=[];
end
......
--------------------------------

to collect the frames into the im array.

You need to initialize things first, using

------------------------
if GENERATE_GIF
f = getframe(gcf);
[im,map] = rgb2ind(f.cdata,256,'nodither');
im(1,1,1,20) = 0;
else
im=[];
map=[];
end
------------------------

At the end, you save the animated gif file as

-------------------
if GENERATE_GIF
imwrite(im,map,'HW2_problem2.gif','DelayTime',0,'LoopCount',inf)
end
--------------------

A full implementation of the above can be seen
on entry #14 here

http://12000.org/my_notes/my_matlab_functions/index.htm

click on the source code there next to the animation.

Another easy way to do all of this is this:

Just run the matlab code as is, and use something like
cam studio to grap the movie from the screen, then use
VirtualDub to save the avi to animated png. All free
software on windows. This is what I do if I want to make
quick animated gif file without changing the code.

--Nasser

Date Subject Author
12/26/13 Dila
12/26/13 Nasser Abbasi
12/26/13 Nasser Abbasi
12/26/13 Dila
1/1/14 Steven Lord