【问题标题】:Merging two figures in Matlab在matlab中合并两个数字
【发布时间】:2010-01-25 17:35:53
【问题描述】:

我目前正在运行一个生成四个单独图表的 Matlab 脚本(如下)。我想组合其中两个图表,以便同时显示。我希望组合的图表在下面的脚本中称为图 2 和图 4。

脚本的唯一输入是包含 6 列的 txt 文件:x 坐标、y 坐标和 4 个变量(Depth [m]、Hsig [m]、Period [sec]、Dir [degrees])

对此的任何帮助将不胜感激。

% Post-process a SWAN wave model output file
%----------------------------------------------------------------------------------------------
%defaults
N_header = 7; % header lines in SWAN file
N_vars = 6;           %output variables in SWAN file
x_origin = 0;    %real world x origin
y_origin = 0;   %real world y origin
quiver_subsample = 6; %sub-sampling factor to make direction plot clearer
rot_angle = 0; %rotation angle to correct any previous rotation for SWAN
island_mask = load('island_mask.txt'); %mask for islands (set land to NaN);

%specify input file
[filename,pathname] = ...
  uigetfile('*.txt', 'Specify SWAN results file (e.g. Scilly.txt) [*.txt]');
  SWANfile = fullfile(pathname,filename);

%read (and ignore) file header lines
fid = fopen(SWANfile);
for i=1:N_header
 head = fgets(fid);
end
%and now get the data
data = fscanf(fid,'%g %g',[N_vars inf]); data = data';
fclose(fid);

%extract the datasets we want, marking any junk values (e.g. dry land)
XP = data(:,1);
YP = data(:,2);

DEPTH = data(:,3);
dudsDEPTH = (DEPTH==-99);
DEPTH(dudsDEPTH) = NaN;

HS = data(:,4);
dudsHS = (HS==-9);
HS(dudsHS) = NaN;

PER = data(:,5);
dudsPER = (PER==-9);
PER(dudsPER) = NaN;

DIR = data(:,6);
dudsDIR = (DIR==-999);
DIR(dudsDIR) = NaN;

minX = min(XP);
minY = min(YP);
maxX = max(XP);
maxY = max(YP);
cellsize = XP(2) - XP(1);

% mesh and plot data onto scaled output grids
[xp,yp] = meshgrid(minX:cellsize:maxX,minY:cellsize:maxY);

sx = size(xp); xlen = sx(2); ylen = sx(1);
depth = reshape(DEPTH,xlen,ylen);
hs = reshape(HS,xlen,ylen);
per = reshape(PER,xlen,ylen);
dir = reshape(DIR,xlen,ylen);

depth_rot = flipud(rot90(depth,1));%pcolor(depth_rot);shading flat
hs_rot = flipud(rot90(hs,1));%pcolor(hs_rot);shading flat
per_rot = flipud(rot90(per,1));%pcolor(per_rot);shading flat
dir_rot = flipud(rot90(dir,1));
%remember that actual directions also need rotating (i.e. not just matrix!)
dir_rot = dir_rot + rot_angle; %pcolor(dir_rot);shading flat

xp_rot = xp;
yp_rot = yp;

%create x and y matrices in real world co-ordinates
xp_rot = xp_rot + x_origin;
yp_rot = yp_rot + y_origin;

%and equivalent x and y vectors, in case we need these instead
grid_cells = size(xp_rot);
x_cells = grid_cells(2); %columns
y_cells = grid_cells(1); %rows
x_utm = x_origin:cellsize:x_origin + (x_cells*cellsize);
y_utm = y_origin:cellsize:y_origin + (y_cells*cellsize); 
% y_utm = fliplr(y_utm); % flip to ensure cartesian rather than image axes


%create bathymetry plot
figure(1)
if ~isempty(island_mask)
 depth_rot_plot = depth_rot;
 depth_rot_plot(island_mask) = NaN;
 imagesc(x_utm,y_utm,depth_rot_plot)
 colormap(jet(256));
 map = colormap;
 map(1,:) = 1;
 % map(2,:) = 1;
 % map(3,:) = 1;
 colormap(map); 
else
 imagesc(x_utm,y_utm,depth_rot)
end
title('Bathymetry (m)', 'fontsize', 12)
set(gca,'fontsize', 12);
axis equal
axis tight
axis xy % need this to ensure cartesian rather than image axes!
colorbar

%create direction plot
figure(2)
[U,V] = pol2cart((dir_rot) ./ (180/pi),ones(size(dir_rot)));
% and sub-sample output grid to produce clearer plot
U_subsample = nestedsubsample2(U,quiver_subsample);
V_subsample = nestedsubsample2(V,quiver_subsample);
X_subsample = nestedsubsample2(xp,quiver_subsample);
Y_subsample = nestedsubsample2(yp,quiver_subsample);
quiver(X_subsample,Y_subsample,U_subsample,V_subsample,'k');
title('Direction')
axis equal
axis tight

%visualise wave breaking by taking ratio of Hs and depth
breaking = hs_rot ./ depth_rot;
breaking(breaking > 0.7) = 0.70;

%create Hb/depth plot to show where waves are shoaling and/or breaking
figure(3)
imagesc(x_utm,y_utm,breaking)
title('Hs / depth', 'fontsize', 12)
set(gca,'fontsize', 12);
axis equal
axis tight
axis xy % need this to ensure cartesian rather than image axes!
colorbar


%create Hs plot
figure(4)
imagesc(x_utm,y_utm,hs_rot)
title('Hs (m)', 'fontsize', 12)
set(gca,'fontsize', 12);
axis equal
axis tight
axis xy % need this to ensure cartesian rather than image axes!
colorbar


function [subsampled_A] = nestedsubsample2(A,I)
%NESTEDSUBSAMPLE2(A,I)
%resample 2D matrix A by retaining every Ith element

if I < 1
 I = 1;
end

A_dim = size(A);
new_i = 1:I:A_dim(1);
new_j = 1:I:A_dim(2);

subsampled_A = A(new_i,new_j);

end


end

【问题讨论】:

  • 。我的眼睛……拜托,格式化代码..
  • 您想要两个并排的人物,还是以某种方式叠加?
  • 您真的不需要在问题中发布整个脚本。一些示例代码让我们了解您的尝试就足够了。
  • 查看我对相关问题的解决方案:stackoverflow.com/questions/4008880/…

标签: matlab merge figure


【解决方案1】:

如果您希望在同一图中在不同的轴上显示多个图表,您可以使用SUBPLOT 命令。这将允许您在图形窗口中平铺轴。

如果您希望多个图表出现在相同的坐标轴上,您可以使用HOLD 命令。

【讨论】:

    【解决方案2】:

    subplot(m,n,p) 让我困惑了一会儿,以防万一 其他人有
    同样的心理障碍,我想我会投入两分钱

    the m,n values set up numbers of rows and columns within a figure
    the p   value  determines which subplot you are accessing 
                   subplots are numbered   1 ..  n in row 1
                                         n+1 .. 2n in row 2
                                        2n+1 .. 3n in row 3   etc
    

    例如,对于您将访问的一组 3 行 2 列的数字 每个位置都是这样的:

    +-----------------------------------+
    | subplot(3,2,1)  |  subplot(3,2,2) |
    |-----------------+-----------------+
    | subplot(3,2,3)  |  subplot(3,2,4) |
    |-----------------+-----------------+
    | subplot(3,2,5)  |  subplot(3,2,6) |
    |-----------------------------------+
    

    在每个subplot(m,n,p) 调用之后,您就可以正常使用plot()title()legend()
    调用并在指定的子图位置绘制图

    【讨论】:

      【解决方案3】:

      这是 gnovice 建议使用 subplot 的简单实现。

      例如,假设您希望数字 3 和 4 在同一个数字窗口中并排:

      figure(3)
      
      %create Hb/depth plot to show where waves are shoaling and/or breaking
      subplot(1,2,1)
      imagesc(x_utm,y_utm,breaking)
      title('Hs / depth', 'fontsize', 12)
      set(gca,'fontsize', 12);
      axis equal
      axis tight
      axis xy % need this to ensure cartesian rather than image axes!
      colorbar
      
      %create Hs plot
      subplot(1,2,2)
      imagesc(x_utm,y_utm,hs_rot)
      title('Hs (m)', 'fontsize', 12)
      set(gca,'fontsize', 12);
      axis equal
      axis tight
      axis xy % need this to ensure cartesian rather than image axes!
      colorbar
      

      我现在无法访问我的 MATLAB 计算机,所以我无法为您检查,但假设您上面编写的代码可以正常工作,这也应该。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 2018-04-22
        • 1970-01-01
        • 2016-11-15
        • 2022-11-02
        相关资源
        最近更新 更多