Error. Movie contains uninitialized frames

8 views (last 30 days)
I'm trying to make a movie with the following code:
x=[-3:0.1:3];
y=[-3:0.1:3];
t=[0:0.1:5];
for i = 1:length(t);
[x,y]=meshgrid(-3:.1:3,-3:.1:3);
z=exp(-(x-t(1,i)).^2-(y-t(1,i)).^2);
surf(x,y,z);
axis('tight')
clear('M');
M(i)=getframe;
end
movie(M);
movie2avi(M,'problem_3.avi');
But it's giving the following error when I run it:
Error using hgMovie
Movie contains uninitialized frames
Please how can I solve this?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 7 Sep 2014
Why are you deleting the M variable in the loop with the clear() function??? Get rid of that.
  1 Comment
Fdee1
Fdee1 on 9 Sep 2014
After deleting "clear ('M')" and starting the code with "clear all". It worked! Thanks for your help.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Sep 2014
Try it this way:
clear all;
clc;
x=[-3:0.1:3];
y=[-3:0.1:3];
t=[0:0.1:5];
hFigure = figure;
numberOfFrames = length(t);
% Set up the movie structure.
% Preallocate recalledMovie, which will be an array of structures.
% First get a cell array with all the frames.
allTheFrames = cell(numberOfFrames,1);
vidHeight = 344;
vidWidth = 446;
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
myMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps);
% Create a VideoWriter object to write the video out to a new, different file.
% writerObj = VideoWriter('problem_3.avi');
% open(writerObj);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
[x, y] = meshgrid(-3:.1:3,-3:.1:3);
for frameIndex = 1 : numberOfFrames
z = exp(-(x-t(frameIndex)).^2-(y-t(frameIndex)).^2);
cla reset;
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
surf(x,y,z);
axis('tight')
zlim([0, 1]);
caption = sprintf('Frame #%d of %d, t = %.1f', frameIndex, numberOfFrames, t(frameIndex));
title(caption, 'FontSize', 15);
drawnow;
thisFrame = getframe(gca);
% Write this frame out to a new video file.
% writeVideo(writerObj, thisFrame);
myMovie(frameIndex) = thisFrame;
end
% close(writerObj);
message = sprintf('Done creating movie\nDo you want to play it?');
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
close(hFigure);
if strcmpi(button, 'No')
return;
end
hFigure = figure;
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
title('Playing the movie we created', 'FontSize', 15);
% Get rid of extra set of axes that it makes for some reason.
axis off;
% Play the movie.
movie(myMovie);
uiwait(helpdlg('Done with demo!'));
close(hFigure);

Categories

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!