Error creating an animated GIF in Matlab

조회 수: 72 (최근 30일)
Wissem-Eddine KHATLA
Wissem-Eddine KHATLA 2022년 2월 15일
답변: Wissem-Eddine KHATLA 2022년 2월 16일
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
Wissem-Eddine KHATLA 2022년 2월 16일
Thank you all for your help : now it works. I totally forgot about this line so thanks for your intervention.
Sincerely,

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by