在 MATLAB 中将输出图形保存为 png 文件

Posted

技术标签:

【中文标题】在 MATLAB 中将输出图形保存为 png 文件【英文标题】:Saving output figures to png files in MATLAB 【发布时间】:2017-12-20 16:19:39 【问题描述】:

我有下面显示的代码,它从文本数据中绘制了一些 2D 图像,这些数据是从单独的数值建模软件(压力文件)中获得的。这段代码询问我一个时间瞬间和一些可视化限制,然后给我一个数字。

我想要做的是,让它要求我输入一个时间间隔(比如从 2​​ 秒到 20 秒)和一个“时间步长”(比如 1 秒),因为可视化对所有人的限制都是一样的数字。然后它应该为我提供 19 个数字左右。最重要的是,我还希望它会自动将输出保存为 .png 文件。

编辑:

clear all;
close all;

flnam = 'parabolic';

flxxx = ([flnam,'_xx.mat']);
flyyy = ([flnam,'_yy.mat']);
flzzz = ([flnam,'_zz.mat']);

xms = load(flxxx);
yms = load(flyyy);
zms = load(flzzz);

xm = xms.Xp;
ym = yms.Yp;
zm = zms.Botlev;

clear xms;
clear yms;
clear zms;

% computational domain limits

x10 = min(min(xm));
y10 = min(min(ym));
x20 = max(max(xm));
y20 = max(max(ym));

dspg = 0; % sponge width

x11 = x10+dspg;
x22 = x20-dspg;
y11 = y10+dspg;
y22 = y20-dspg;

% display bottom

zm(zm==-99) = NaN;

zm(xm<x11|xm>x22|ym<y11|ym>y22) = NaN;

zmin = min(min(zm));
zmax = max(max(zm));

disp(' ');
disp(' * bathymetry visualization *')

disp(' ');
disp(['   minimum depth : ', num2str(zmin)]);
disp(['   maximum depth : ', num2str(zmax)]);

disp(' ');
vvmin = input('   new minimum depth ? ');
vvmax = input('   new maximum depth ? ');
dvv   = input('   contour interval ?  ');

figure(500);

vv = (vvmin:dvv:vvmax);

[cc,hh] = contourf(xm,ym,zm);
set(hh,'LineColor','none')
axis equal;
axis([x11 x22 y11 y22]);
caxis([vvmin vvmax]);
colorbar;

% reads surface elevation

flwav = ([flnam,'_eta.mat']);

e3m = load(flwav);

disp(' ');
disp(' ');
disp(' * wake waves visualization *')
disp(' ');
kt = input(' time instant (s) ');

kft = 0;

while kt >= 0,

   % get time format

   tt = kt;

   kft = kft + 1;

   % hours
   th  = floor(tt/3600);
   sth = num2str(th,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
   % minutes
   tt  = tt-th*3600;
   tm  = floor(tt/60);
   stm = num2str(tm,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
   % seconds
   tt = tt-tm*60;
   ts = floor(tt);
   sts = num2str(ts,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
   % thousandth of second
   tt = tt-ts;
   tms  = round(tt*1000);
   stms = num2str(tms,'%3.3i'); % string with 3 digits (e.g. '003' instead of '3')
   % time string
   hhmmss = ([sth,stm,sts,'_',stms]);
   fig_title = ([sth,'h',stm,'min',sts,'.',stms,'s']);

   sfield = (['Watlev_',hhmmss]);

   yesno = isfield(e3m,sfield);

   % if SWASH got a small error in the time stamping of fields within the "e3m" struct
   if yesno == 0,
      if tms == 0,
     tms = 999;
     if ts == 0,
        ts = 59;
        if tm == 0,
           tm = 59;
           th = th-1;
               sth = num2str(th,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
        else
           tm = tm-1
        end
            stm = num2str(tm,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
     else
        ts = ts-1;
     end
         sts = num2str(ts,'%2.2i'); % string with 2 digits (e.g. '03' instead of '3')
      else
         tms = tms-1;
      end
      stms = num2str(tms,'%3.3i'); % string with 3 digits (e.g. '003' instead of '3')
      hhmmss = ([sth,stm,sts,'_',stms]);
      sfield = (['Watlev_',hhmmss]);
   end

   % surface elevation
   em = e3m.(genvarname(sfield));

   em(em==-99) = NaN;

   em(xm<x11|xm>x22|ym<y11|ym>y22) = NaN;

   emin = min(min(em));
   emax = max(max(em));

   disp(' ');
   disp(['   minimum surface elevation : ', num2str(emin)]);
   disp(['   maximum surface elevation : ', num2str(emax)]);

   disp(' ');
   vvmin = input('   new minimum ? ');
   vvmax = input('   new maximum ? ');
   dvv   = input('   contour interval ?  ');

   em(em<=vvmin) = vvmin;

   kfig = figure(kft);

   % set figure's size and position in the screen
   set(kfig,'units','centimeters'); % sets units to centimeters
   posk = get(kfig,'pos'); % to see the default values
   xf0 = 3; % x-coordinate (in the screen) of the left lower corner
   yf0 = 0; % y-coordinate (in the screen) of the left lower corner
   xflen = 21; % x-length of figure
   yflen = 21; % y-length of figure
   set(kfig,'pos',[xf0 yf0 xflen yflen]);

   vv = (vvmin:dvv:vvmax);

   nc = size(vv,2);

   my_colormap = load('bluered.cmp');

   [cc,hh] = contourf(xm,ym,em,vv);
   set(hh,'LineColor','none')
   title(fig_title);
   axis equal;
   axis([x11 x22 y11 y22]);
   caxis([vvmin vvmax]);
   colorbar;
   colormap(my_colormap);

   kfig = figure(kft+100);

   % set figure's size and position in the screen
   set(kfig,'units','centimeters'); % sets units to centimeters
   posk = get(kfig,'pos'); % to see the default values
   xf0 = 3; % x-coordinate (in the screen) of the left lower corner
   yf0 = 0; % y-coordinate (in the screen) of the left lower corner
   xflen = 21; % x-length of figure
   yflen = 21; % y-length of figure
   set(kfig,'pos',[xf0 yf0 xflen yflen]);

   [cc,hh] = contour(xm,ym,em,vv);
   set(hh,'LineColor','k')
   title(fig_title);
   axis equal;
   axis([x11 x22 y11 y22]);
   caxis([vvmin vvmax]);

   disp(' ');
   disp(' Next case !');
   disp(' ');
   kt = input(' time instant (s) ');

end

% end program view_wake_2DH_mat_geral

【问题讨论】:

复制并粘贴问题中的代码,然后将其全部突出显示并按 CTRL+ K。除非我们看到代码,否则我们无能为力。另请记住,*** How to Ask 页面上的一条规则指出:仅包含足够的代码以允许其他人重现该问题。如需帮助,请阅读How to create a Minimal, Complete, and Verifiable example。因此,即使代码很长,我们也需要足够的内容来重现您的问题......您还没有完成。如果不这样做,您的帖子更有可能被选为离题。 感谢您的提示!刚刚编辑了帖子,现在你应该可以看到代码了。 【参考方案1】:

只需使用saveas function。示例用法:

saveas(gcf,'figure.png');

或者,直接引用:

fig_1 = figure();
% your plotting...
saveas(fig_1,'figure_1.png');

【讨论】:

谢谢,保存图的部分。你知道时间间隔吗? 那部分我不太清楚,你能详细说明一下吗?

以上是关于在 MATLAB 中将输出图形保存为 png 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在循环的每次迭代中将新图形保存为 png

如何在 Matlab 中将线阵列保存为图形

Python-Matplotlib可视化——图形的输出与保存

MATLAB:使用默认名称保存图

在 MATLAB 中将频谱图另存为图像

在 MATLAB 中将动画变形另存为 GIF 文件