我有一个例子,说明我如何生成每个包含2个子图的图形(我必须生成数百个):
figure
axes('Position',[left1 bottom1 width1 height1]);
for i = 1:3:15
... code to generate upper figure
end
axes('Position',[left2 bottom2 width2 height2]);
for j = 5:9
... code to generate bottom figure
end
根据' for'循环,我应该得到5个数字,如下一个,但我只得到一个(最后一个),我真的不知道为什么:
任何人都知道如何优化我的代码以生成所有数字并将它们另存为.png或.jpg?
答案 0 :(得分:4)
尝试将以下伪代码编写到Matlab结构中:
for count = 1:number_of_figures
figure(count);
axes('Position',[left1 bottom1 width1 height1]);
for i = 1:3:15
subplot(2,1,1);
... code to generate upper figure
end
axes('Position',[left2 bottom2 width2 height2]);
for j = 5:9
subplot(2,1,2);
... code to generate bottom figure
end
saveas(gcf,'.png');
end
要在matlab中保存图像,请使用saveas
,这也允许您指定格式(.jpg,.png等)。
答案 1 :(得分:3)
我想这就是你需要的。
如果你想在2个单独的数字中显示上图像和底部图像:
f1 = figure();
count = 1; %Temporary value
axes('Position',[left1 bottom1 width1 height1]);
for i = 1:3:15
subplot(1,5,count);
....code to generate upper figure
count = count+1;
end
f2 = figure();
count = 1; %Temporary value
axes('Position',[left2 bottom2 width2 height2]);
for j = 5:9
subplot(1,5,count);
....code to generate bottom figure
count = count+1;
end
saveas(f1, 'image1.png');
saveas(f2, 'image2.png');
如果您想要同一图中的上图像和下图像:
f1 = figure();
count = 1; %Temporary value
axes('Position',[left1 bottom1 width1 height1]);
for i = 1:3:15
subplot(2,5,count);
....code to generate upper figure
count = count+1;
end
count = 6; %Temporary value
axes('Position',[left2 bottom2 width2 height2]);
for j = 5:9
subplot(2,5,count);
....code to generate bottom figure
count = count+1;
end
saveas(f1, 'image.png');
如果您只需要一个图中的一个上图像和下图像:
a = 1:3:15;
b = 5:9;
for k = 1:5
f = figure(k);
subplot(2,1,1);
axes('Position',[left1 bottom1 width1 height1]);
i = a(k);
... code to generate upper figure
subplot(2,1,2);
axes('Position',[left2 bottom2 width2 height2]);
j = b(k);
... code to generate bottom figure
print('-dpng','-r800',sprintf('image%d.png',k));
end