MATLAB Answers

create a gif or video using this sequence of figures

72 views (last 30 days)
Alberto Acri
Alberto Acri on 26 Dec 2020
Commented: Walter Roberson on 29 Dec 2020
I want to create a gif or video using this sequence of figures:
plot of figure 1
pause(0.8);
hold on;
plot of figure 2;
pause(0.8);
plot of figure 3;
pause(0.8);
plot of figure 4;
pause(0.8);
hold off
Is it possible ?

  7 Comments

Show 4 older comments
Walter Roberson
Walter Roberson on 27 Dec 2020
n = 1:0.5:5
That is a vector of values
if n == 1
You are testing a vector of values. For the if to succeed, all of the tests need to be true simultaneously . The first element where n is 1 succeeds, but where n = 1.5, 2, 2.5, and so on all fail, so n == 1 is [true] followed by a bunch [false], and if needs them all to be true to work. So the if will not match. Only the else will happen.
The else branch will write one frame.
You do not have any looping.
Also, the colormap needs to be the same for all of the frames. That is not a requirement of GIF file format, but imwrite() is not able to write different colormaps for each frame, so imwrite() would silently rewrite your coloring. I at least made it obvious here. Now you just have the problem that the colormap derived from the first image might not be representative of the rest. You would probably be better off using a fixed colormap.
% Capture the plot as an image
h=figure
filename = 'testAnimated.gif';
for n = 1:0.5:5
frame = getframe(h);
im = frame2im(frame);
% Write to the GIF File
if n == 1
[imind,cm] = rgb2ind(im,256);
imwrite(imind, cm, filename, 'gif', 'Loopcount', inf);
else
imind = rgb2ind(im, cm);
imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append');
end
Alberto Acri
Alberto Acri on 27 Dec 2020
but I want to create a video with the sequence of images above.
Walter Roberson
Walter Roberson on 27 Dec 2020
Your question asked to create a gif or a video, and the above shows creating a gif.

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 29 Dec 2020
Hi,
Apart from all the resources provided in the comments above, the below links would surely help you -
1.) Link 1
2.) Link 2
The above 2 links contain similar questions answered by MATLAB users and accepted too.

  2 Comments

Alberto Acri
Alberto Acri on 29 Dec 2020
I used link 1 shown below:
N=1 % set
for i = 1:N
figure(1)
% imshow(processo(:,:,1,i))
plot of figure 1
pause(0.8);
hold on;
plot of figure 2;
pause(0.8);
plot of figure 3;
pause(0.8);
plot of figure 4;
pause(0.8);
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);
but it creates me a video of 00.00.00 seconds. I set N=1
Walter Roberson
Walter Roberson on 29 Dec 2020
You need more than one frame for avi files. It is a limitation of the movie encoders.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!