Get figures and use them to build a video.avi

Hello, I have developed an algorithm that shows at every iteration a figure as follow:
figure
imshow(processo(:,:,1,i))
hold on
plot(X,Y,'o')
plot(X0,Y0,'o')
plot(X1,Y1,'o')
plot(X2,Y2,'o')
plot(X3,Y3,'o')
end
What I need is to save in an array each figure (i.e. imshow with each plot) and build an .avi video from them. Any suggestions?
Thanks in advance, Davide

 Accepted Answer

for i = 1:N
figure(1)
imshow(processo(:,:,1,i))
hold on
plot(X,Y,'o')
plot(X0,Y0,'o')
plot(X1,Y1,'o')
plot(X2,Y2,'o')
plot(X3,Y3,'o')
hold off
F(i) = getframe(gcf) ;
drawnow
end
% create the video writer with 1 fps
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% set the seconds per image
% open the video writer
open(writerObj);
% write the frames to the video
for i=1:length(F)
% convert the image to a frame
frame = F(i) ;
writeVideo(writerObj, frame);
end
% close the writer object
close(writerObj);

7 Comments

Davide Magnelli commented:
Thanks a lot!!!!!
Dongik Lee commented: Thanks a lot!!!!!
I am using the same type of code but I am getting a frame error at the folowing line:
I have tried minimizing the frame size of figure but the same error persists!
>>
Error using VideoWriter/writeVideo (line 383)
Frame must be 119 by 99
Error in tst (line 59)
writeVideo(writerObj, frame);
>>
responding to Dharaneedharan Arumugam in case someone else has this problem: you need to make sure your image doesn't change size after the first frame. Updating a plot (or especially in 3D) can change the field of view.
I get the error
```
Dot indexing is not supported for variables of this
type.
Error in alternateGetframe
Error in getframe (line 136)
x = alternateGetframe(parentFig, offsetRect,
scaledOffsetRect, includeDecorations);
Error in graph (line 41)
F(i) = getframe(gcf);
```
x=[0 0 199 99];
frame=getframe(gcf,x);

Sign in to comment.

More Answers (2)

For what it's worth, I'm attaching my demo for creating a video from different surfaces made with the surf() function.

Asked:

on 26 Feb 2018

Answered:

on 31 Mar 2022

Community Treasure Hunt

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

Start Hunting!