Error creating an animated GIF in Matlab

82 次查看(过去 30 天)
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,

采纳的回答

DGM
DGM 2022-2-15
编辑:DGM 2022-2-15
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

更多回答(2 个)

yanqi liu
yanqi liu 2022-2-16
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
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!

Translated by