All figures in subplot saved to animated gif file.

11 ビュー (過去 30 日間)
David
David 2018 年 2 月 28 日
回答済み: David 2018 年 2 月 28 日
Hi,
I am attempting to save a sequence of figures created using the subplot() function to a .gif animation.
Currently, only one plot of the two presented in subplot is saved. The plot also does not have a title or xtick labels.
What modifications to my code do I need to make in order to save the subplot as it appears on my screen to the .gif file?
for ii = 1:5
subplot(1,2,1);
imagesc(rand(10))
title(num2str(ii))
daspect([1 1 1])
subplot(1,2,2)
imagesc(rand(10))
title(num2str(ii))
daspect([1 1 1])
filename = 'saved_gif.gif';
del = 0.1; % time between animation frames
drawnow
frame = getframe(gca);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1;
imwrite(imind,cm,filename,'gif','Loopcount',inf,'DelayTime',del);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',del);
end
end

採用された回答

David
David 2018 年 2 月 28 日
I think I have solved this myself. I will leave the answer here in case anyone else has a similar problem.
Ammended code:
fig_num = 1; % sets number of figure to be opened
for ii = 1:5
figure(fig_num)
subplot(1,2,1);
imagesc(rand(10))
title(num2str(ii))
daspect([1 1 1])
subplot(1,2,2)
imagesc(rand(10))
title(num2str(ii))
daspect([1 1 1])
filename = 'saved_gif.gif';
del = 0.1; % time between animation frames
drawnow
frame = getframe(fig_num);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1;
imwrite(imind,cm,filename,'gif','Loopcount',inf,'DelayTime',del);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',del);
end
end
Essentially, you need only to declare a figure number and call that number with getframe later in the script.
I hope this is helpful.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by