在MATLAB中将输出数字保存到png文件

时间:2017-12-20 16:19:40

标签: image matlab save

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

我想要做的是,请求我输入一个输入,我会给出一个时间间隔(例如从2s到20s)和一个“时间步长”(比如1s),因为可视化限制所有人都一样数字。那么它应该给我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

1 个答案:

答案 0 :(得分:0)

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

saveas(gcf,'figure.png');

或者,直接引用:

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