Error creating an animated GIF in Matlab
81 views (last 30 days)
Show older comments
Wissem-Eddine KHATLA
on 15 Feb 2022
Answered: Wissem-Eddine KHATLA
on 16 Feb 2022
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,
0 Comments
Accepted Answer
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
0 Comments
More Answers (2)
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
0 Comments
See Also
Categories
Find more on Get Started with MATLAB 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!