How to save figure frames as gif file?

조회 수: 307(최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2022년 4월 12일
답변: Ashfaq Ahmed 2022년 4월 13일
Hi all,
I am having a problem while saving my figure frames as a gif file. I am using this code -
for i = 1:Leng
figure(1);
hold on
title(['Time = ' num2str(t(i))]);
plot(px(i,:),py(i,:),'o','MarkerSize',4,'color',[1 0 0]);
pause(0.2);
F(i) = getframe(gcf);
drawnow
end
%Making mnovie
writerObj = VideoWriter('Particle02.gif');
writerObj.FrameRate = 10;
open(writerObj);
% write the frames to the video
for i=1:length(F)
frame = F(i) ;
writeVideo(writerObj, frame);
end
% close the writer object
close(writerObj)
Although I am saving it as '.gif' file, it is being saved as '.avi' file.
Can anyone please help? Any feedback will be much appreciated!

채택된 답변

Image Analyst
Image Analyst 2022년 4월 12일

추가 답변(1개)

Ashfaq Ahmed
Ashfaq Ahmed 2022년 4월 13일
Thanks! I actually solved it by using this script -
for i = 1:Leng
figure(1);
filename = 'Day02_Path.gif';
set(gcf,'Position',[-1754,65,1166,1272],'Resize','off');hold on
xlim([-2050099.543264665 -1948716.788232876]);
ylim([444467.6319204442 585529.1910998748]);
hold on;
title('02-April-2015','fontsize',20);
%subtitle(['Time (in seconds) = ' num2str(t(i))],'fontsize',20);
plot(px(i,:),py(i,:),'o','MarkerSize',5,'color',[0 0 1]);
pause(0.02);
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
scalebarpsn('location','sw'); hold on;
end

Community Treasure Hunt

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

Start Hunting!

Translated by