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 Center および File ExchangeLighting, 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