【问题标题】:Combining individual MATLAB plots into one figure: is modifying the script the best option?将单个 MATLAB 绘图组合成一个图形:修改脚本是最佳选择吗?
【发布时间】:2020-02-04 02:33:45
【问题描述】:

下面的脚本生成七个单独的图形(每个图形包括两个图)。我想将它们组合成一个包含所有 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

【问题讨论】:

  • 组合成一个图是什么意思?您想将每个图添加到相同的两个轴上,还是想要一个包含所有图的 7x2 轴网格?
  • 我想要一个包含 14 个图的最终大图(所以是的,7x2 轴)。谢谢你的慰问;我现在可以看到我的问题应该写得更清楚了。
  • 是的,需要修改代码。 subplot(n, 2, 7) 将绘制到 2x7 绘图数组中的 nth 绘图。我希望在你的循环中计算n 非常简单。

标签: matlab for-loop plot matlab-figure subplot


【解决方案1】:

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

hold on

就在函数figure和

之后
hold off

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

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

【讨论】:

  • 这很遗憾没有帮助!输出仍然相同:7 个不同的图形(每个包含 2 个子图)。我还尝试插入hold on、hold off(在建议的位置)并将figure 更改为figure (1)。在这种情况下,输出是一个带有 2 个子图的图形。但是第二个子图包含所有相互重叠的痕迹......我真正需要实现的是一个包含所有 7 个图的大图(总共 14 个图)。
猜你喜欢
  • 1970-01-01
  • 2019-04-09
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
相关资源
最近更新 更多