Clear Filters
Clear Filters

Error creating an animated GIF in Matlab

82 views (last 30 days)
Hello everyone,
I am trying to create a GIF showing the evolution of a height field (variable noted "h_reel") through time.
Unfortunately, I am encoutering an error and I am unable to performe what I am attempting to do... Here is my script :
p = figure(21);
filename = 'test.gif';
for i = index_intersection:nfichier
imagesc(h_reel(:,:,i));
[X, Y] = meshgrid([1:2048]*echelle,[1:2048]*echelle);
surf(X,Y,h_reel(:,:,i));
caxis([min(h_reel,[],'all') max(h_reel,[],'all')])
title(num2str(i))
shading interp;
%pause 0.25;
frame = getframe(p);
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
end
Obtaining this error :
Error using matlab.internal.imagesci.wgifc
Can only append to GIF89a format GIFs.
Error in writegif (line 306)
matlab.internal.imagesci.wgifc(mat, map, filename,writemode,disposalmethod,delaytime,...
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Could someone help me to solve this problem please ?
Thank you in advance.
Sincerely,

Accepted Answer

DGM
DGM on 15 Feb 2022
Edited: DGM on 15 Feb 2022
This problem happens when you try to append to a file that doesn't already exist.
for i = index_intersection:nfichier
% ...
end
If index_intersection is not 1, then the case for writing the first frame won't happen.
You can try something like this.
indices = index_intersection:nfichier;
for framenum = 1:numel(indices)
imagesc(h_reel(:,:,framenum));
[X, Y] = meshgrid([1:2048]*echelle,[1:2048]*echelle);
surf(X,Y,h_reel(:,:,framenum));
caxis([min(h_reel,[],'all') max(h_reel,[],'all')])
title(num2str(framenum))
shading interp;
%pause 0.25;
frame = getframe(p);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if framenum == 1
imwrite(imind,cm,filename,'gif','LoopCount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end

More Answers (2)

yanqi liu
yanqi liu on 16 Feb 2022
yes,sir,may be add some check flag,such as
p = figure(21);
filename = 'test.gif';
check_flag = 0;
for i = index_intersection:nfichier
imagesc(h_reel(:,:,i));
[X, Y] = meshgrid([1:2048]*echelle,[1:2048]*echelle);
surf(X,Y,h_reel(:,:,i));
caxis([min(h_reel,[],'all') max(h_reel,[],'all')])
title(num2str(i))
shading interp;
%pause 0.25;
frame = getframe(p);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% use check
if check_flag == 0
imwrite(imind,cm,filename,'gif','LoopCount',inf,'DelayTime',0.2);
check_flag = 1;
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.2);
end
end

Wissem-Eddine KHATLA
Wissem-Eddine KHATLA on 16 Feb 2022
Thank you all for your help : now it works. I totally forgot about this line so thanks for your intervention.
Sincerely,

Community Treasure Hunt

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

Start Hunting!