Animating a sine wave movie

76 views (last 30 days)
tyler brecht
tyler brecht on 23 Oct 2014
Commented: Image Analyst on 12 May 2016
Hi guys I would like to know how to make a movie of a sine wave using handle graphics and for each frame a for loop with getframe
a = -1; z = 0:pi/1000:2*pi; y = a*sin(z)

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2014
See my attached demo. Just change the surf() call to plot() and it should work.

More Answers (1)

Ced on 23 Oct 2014
Edited: Ced on 23 Oct 2014
Something like that? (reduced the number of frames)
clear all
close all
a = -1;
z = 0:pi/100:2*pi;
y = a*sin(z);
Nz = length(z);
% Set up your plot environment
h = plot(z(1),y(1));
xlim([z(1) z(end)])
ylim([min(y) max(y)])
legend('my sine function')
init_getframe = struct('cdata',[],'colormap',[]);
frames = repmat(init_getframe, Nz, 1 );
frames(1) = getframe;
% Get frames
for i = 2:Nz
frames(i) = getframe;
% Play movie
But from experience, I prefer to save each frame to a file (e.g. .png), and then combine the frames later into a movie.
*EDIT*: I was a bit too slow. Make sure to check out Image Analysts answer for some extra frame size settings etc.
Image Analyst
Image Analyst on 12 May 2016
Set the framerate property of the video writer.

Sign in to comment.


Find more on Animation 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!