我可以在一个循环中在多个 Matlab 图形中制作多个子图吗?



我想以两个数字导出我的数据,每个数字有四个子图。但是当我尝试在循环中执行此操作时,它只打印具有四个图的第二个图形。当我使用figure时,它会打印八个数字,每个数字有一个绘图。这是代码的一部分:

subplot(2,2,k);
plot(2.^[4:2:10], a, '-mo', 2.^[4:2:10], b, '-r+', 2.^[4:2:10], c, '-bx'  );
axis([2.^4, 2.^10, 0, max([max(a), max(b), max(c)])]);    
str = sprintf('Time for m1 = 2^%d',i);
title(str);
xlabel('n ')
ylabel('s') 
subplot(2,2,k);
plot(2.^[4:2:10],a1, '-mo', 2.^[4:2:10], b1, '-r+', 2.^[4:2:10], c1, '-bx'  );
axis([2.^4, 2.^10, 0, max([max(a1), max(b1), max(c1)])]);    
str = sprintf('Time for m1 = 2^%d',i);
title(str);
xlabel('n ')
ylabel('M') 

你的循环需要看起来像这样:

x = 1:2;
y = x;
f = 2;  %number of figures
c = 2;  %number of plots per column per figure
r = 2;  %number of plots per row per figure
n = repmat(cumsum(ones(1,r*c)),1,f);  %index for subplots
h = ceil( (1:f*r*c)/(r*c) ); %index of figures
for ii=1:f*r*c
   % calculations
   % plot specifier
   figure( h(ii) )
   subplot( r,c,n(ii) )
   % plot
   plot(x,y)
   % your plot properties
end

它为您提供了一个具有 2x2 子图的figure(1)和一个具有 2x2 子图的figure(2)

例如

f = 3;  %number figures
c = 3;  %number of columns per figure
r = 4;  %number of rows per figure

会给你 3 个数字,每个数字有 3x4 个图,依此类推......


如果图的显示顺序很重要,则可以更改创建hn的方式。这些只是例子。基本上,它们只是将索引ii与外观顺序相关联的向量。

相关内容

最新更新