我想将数据导出为两个图形,每个图形有四个子图。但是当我尝试在循环内执行此操作时,它会仅打印带有四个图的第二个图。当我使用 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 的图,依此类推...
如果绘图的显示顺序很重要,您可以更改 h
和 n
的创建方式。这些只是示例。基本上,它们只是将索引 ii
与出现顺序相关联的向量。
关于matlab - 我可以在一个循环内的多个 Matlab 图形中制作多个子图吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19840814/