How can I make an animation out of contourf plots?

조회 수: 42 (최근 30일)
David Kaeser
David Kaeser 2021년 11월 8일
댓글: David Kaeser 2021년 11월 9일
Hi,
I am trying to animate my solutions of a 2D convection heat transfer problem over the different time steps using contourf. I would like to make an animation over the time steps to visualize the solution. I have the data for the different time steps in the format T_1.bin, where the number indicates the particular time step.
At the moment I represent the current function (S) and the temperature field (T) of a time step in the following way:
figure(1)
fid = fopen('T.bin');
nx = fread(fid,1,'int32');
ny = fread(fid,1,'int32');
T = reshape(fread(fid,nx*ny,'double'),nx,ny);
fclose = (fid);
contourf(T')
figure(2)
fid = 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')
How is the best way to make an animation out of this?
Thanks a lot already in advance for all your help and time.
  댓글 수: 1
David Kaeser
David Kaeser 2021년 11월 9일
I tried now to capture the results in gif by modifying the succested code to this:
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 now 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?

댓글을 달려면 로그인하십시오.

채택된 답변

Chunru
Chunru 2021년 11월 8일
편집: Chunru 2021년 11월 9일
figure
for t=10:10:100 % t is the percent number in the file name
subplot(121)
fn = "T_timestep_"+t"_precent";
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')
subplot(122)
fn = "S_timestep_"+t"_precent";
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')
drawnow
pause(0.1)
end
  댓글 수: 5
Chunru
Chunru 2021년 11월 9일
See the updated above.
David Kaeser
David Kaeser 2021년 11월 9일
Thank you very much!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by