首页 > 解决方案 > 将单个 MATLAB 图组合成一个图形:修改脚本是最佳选择吗?

问题描述

下面的脚本生成七个单独的图形(每个图形包括两个图)。我想将它们组合成一个包含所有 14 个地块的大图。我认为可能有两种解决方案:要么修改当前脚本,要么在它们已经绘制后将它们添加到一个中。我是个初学者;任何简单的解决方案将不胜感激。(我的数据:https ://www.dropbox.com/s/ggdh03lo10w85vv/Block-1.mat?dl=0 )。

%% loop through frequencies, plot both channels next to each other
% desired # of points
nPoints = 200;

%vary this value to determine how closely you want the plots to be stacked on one another
stackvar = 0.00003;

% loop through frequencies
for f = 1:nf

   % get channel 1 and 2 data for all attenuations (levels)
   % and desired frequency converted

   % from cell to matrix. time in rows, levels in columns
   datatoplot_ch1 = cell2mat(Ch1D.mean(:, f)');
   datatoplot_ch2 = cell2mat(Ch2D.mean(:, f)');

   % plot in new figure for each frequency
   figure

   % loop through levels for this frequency
   for ii = 1:nl

      % plot fresh trace in first plot

      if ii == 1
         % ch 1
         subplot(1, 2, 1)
         plot(datatoplot_ch1(1:nPoints, ii) + stackvar*(nl - (ii - 1)), 'b');

         % ch 2
         subplot(1, 2, 2)
         plot(datatoplot_ch2(1:nPoints, ii) + stackvar*(nl - (ii - 1)), 'r');
      else

         % channel 1
         subplot(1, 2, 1)
         hold on
         plot(datatoplot_ch1(1:nPoints, ii) + stackvar*(nl - (ii - 1)), 'b');
         hold off

         % channel 2
         subplot(1, 2, 2)
         hold on
         plot(datatoplot_ch2(1:nPoints, ii) + stackvar*(nl - (ii - 1)), 'r');
         hold off
      end

      % add level label to identify trace
      subplot(1, 2, 1)
      text(1, stackvar*(nl - (ii - 1)), sprintf('%d', attens(ii)));
      subplot(1, 2, 2)
      text(1, stackvar*(nl - (ii - 1)), sprintf('%d', attens(ii)));
   end

   subplot(1, 2, 1)
   title(sprintf('%s Ch1 %d', BLOCK, freqs(f)));
   subplot(1, 2, 2)
   title(sprintf('%s Ch2 %d', BLOCK, freqs(f)));

end

标签: matlabfor-loopplotmatlab-figuresubplot

解决方案


在你已经绘制它们之后,你不能轻易地将它们“合并”成一个图形。你可以做的是放置命令

hold on

在函数之后figure

hold off

最后之后title。这将确保所有绘图都绘制在当前活动图形上。

有关该函数的更多详细信息,请参阅 MATLAB 文档holdhttps ://www.mathworks.com/help/matlab/ref/hold.html


推荐阅读