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

 Akzeptierte Antwort

Image Analyst
Image Analyst am 7 Sep. 2014

0 Stimmen

Why are you deleting the M variable in the loop with the clear() function??? Get rid of that.

1 Kommentar

Fdee1
Fdee1 am 9 Sep. 2014
After deleting "clear ('M')" and starting the code with "clear all". It worked! Thanks for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 8 Sep. 2014

0 Stimmen

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);

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Sep. 2014

Kommentiert:

am 9 Sep. 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by