How can I make a gif using subplots and avoid an error in function wgifc?

10 次查看(过去 30 天)
Hi everyone,
I am trying to generate a gif of my results over different timestep using the following procedure:
h = figure;
filename = 'testnew51.gif';
axis tight manual % this ensures that getframe() returns a consistent size
for t=10:10:100 % t is the percent number in the file name
subplot(121)
fn = "T_timestep_"+t+"_precent.bin";
fid = fopen(fn); % file for t-th step
nx = fread(fid,1,'int32');
ny = fread(fid,1,'int32');
T = reshape(fread(fid,nx*ny,'double'),nx,ny);
fclose = (fid);
contourf(T')
colorbar;
subplot(122)
fn = "S_timestep_"+t+"_precent.bin";
fid = fopen(fn); % file for t-th stepfid = fopen('S.bin');
nx = fread(fid,1,'int32');
ny = fread(fid,1,'int32');
S = reshape(fread(fid,nx*ny,'double'),nx,ny);
fclose = (fid);
contourf(S')
colorbar;
drawnow
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if t == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
pause(0.2)
end
However I get the error:
Error using wgifc
Can only append to GIF89a format GIFs.
Error in writegif (line 306)
wgifc(mat, map, filename,writemode,disposalmethod,delaytime,...
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in matlab_run (line 34)
imwrite(imind,cm,filename,'gif','WriteMode','append');
How can I resulve that? Or is there a better way to generate a gif?
And also how can I scale the x-axis and y-axis of the subplots in respect of an scaled entity of nx and ny respectively? So that the ratio of the subplots are representing my original grid.
Thanks a lot already in advance for all your help and time.

采纳的回答

Walter Roberson
Walter Roberson 2021-11-17
Your t==1 test is never successful because your for loop is t=10:10:100 and so starts with 10 instead of 1

更多回答(1 个)

Jan
Jan 2021-11-10
编辑:Jan 2021-11-10
The calling style "subplot(121)" is outdated for over 20 years now. Use subplot(1, 2, 1) instead.
You can simplify:
reshape(fread(fid,nx*ny,'double'),nx,ny)
to
fread(fid, [nx, ny],'double')
This line redefines the command fclose as a variable:
fclose = (fid);
You want this instead:
fclose(fid);
I guess, that the number of unclosed files stops opening a new file, because the number of simultaneously open files is limited by the operating system. The error message would be misleading, if this is true.
Use fclose('all') in the command window to clean up the already open files or restart Matlab.
how can I scale the x-axis and y-axis of the subplots in respect of an scaled entity of nx and ny respectively? So that the ratio of the subplots are representing my original grid.
I'm not sure, what this means. Maybe:
axesH = subplot(1, 2, 1);
axis(axesH, 'equal');
  18 个评论
Walter Roberson
Walter Roberson 2021-11-16
To check: are you doing the isfile() before you do the imwrite 'loopcount' call, or after that call? The file is not expected to exist until after the call.
David Kaeser
David Kaeser 2021-11-16
I tired it as statement just after the "loopcount" call in the script and as well outside of the script after an unsucessful run. When I tried as statement in the script the error message already appeared and the statment is not executed. Outside of the script I get a zero value.
I discouvered however something strange now. If I run your code first Walter Roberson and then my code with the same filename the error disaspears. I get then an gif with the results first from the first code and in the same gif the results from my code. How can make sure that I get only my results in the gif?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by