Create a .gif from series of images created in a for loop

11 views (last 30 days)
Hi,
So I have some working code that reads in some data files, does some manipulation on said data and creates an image file based up the results. I can save each image generated into a specified output file, yet I also wish to create a .gif, where each frame is the image that just got generated. The code I currently have seems to just not work, and only uses the 1st image created. My code is below.
Any help would be greately appriciated!
cd('........')
output_file='.......';
gif_name = 'test.gif';
for count = 0:50;
%DATA MANIPULATION
..................
%CREATE AND SAVE IMAGE
data_figure = figure('Visible','off');
axes1 = axes('Parent', data_figure, 'ZGrid', 'on', 'YGrid', 'on');
view(axes1,[0.5 90]);
xlim(axes1,[-0.01 0.25]);
hold(axes1, 'all');
surf(data_A,data_B,data_C, 'Edgecolor', 'none');
colorbar;
xlabel('........');
ylabel('.........');
set(gca, 'XGrid', 'off');
ylabel(colorbar, '.......');
saveas(gcf, fullfile(output_file, [num2str(count),'.png']));
%PART THAT DOESNT WORK....CREATING GIF
frame = getframe(1);
im = frame2im(frame);
[AA,map] = rgb2ind(im,256);
if count == 0,
imwrite(AA,map,gif_name,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(AA,map,gif_name,'gif','WriteMode','append','DelayTime',1);
end
end
  1 Comment
David
David on 15 Aug 2014
Ok I have gotten it to work by changing frame = getframe(1); to frame=getframe(data_figure);
yet it diplays each frame on screen as it is creating the file, how do I stop this?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 Aug 2014
getframe() pulls the image out of the axes. If you don't want it to be displayed, then you can't use getframe(). You'll have to create the image yourself somehow.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!