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

时间:2013-11-07 16:08:04

标签: matlab matlab-figure subplot

我想将数据导出两个数字,每个数据包含四个子图。但是当我尝试在循环中执行此操作时,它仅使用四个绘图继续打印第二个数字。当我使用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') 

1 个答案:

答案 0 :(得分:3)

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

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

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

,例如

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

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


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

相关问题