【问题标题】:Generating and saving many subplot figures in MATLAB在 MATLAB 中生成和保存许多子图图形
【发布时间】:2015-05-31 14:50:25
【问题描述】:

我有一个示例说明如何生成每个包含 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个类似down的数字,但我只得到一个(最后一个),我真的不知道为什么:

有人知道如何优化我的代码以生成所有数字并将它们保存为 .png 或 .jpg 吗?

【问题讨论】:

标签: matlab


【解决方案1】:

尝试将以下伪代码写入您的 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 等)。

【讨论】:

  • 我已经尝试将figure 语句放入循环中,但我只得到了单个数字,而不是整个子图。
  • 您能详细说明一下吗? “整个子图”是什么意思?是否要单独存储每个子图?
  • 我指的是同一个子图中的上图和下图。使用循环内的figure 语句,它只分别运行上图和下图。
  • 您可能需要一个外部循环(如果我正确理解您的问题)。我会编辑答案
  • 如果您能够准确地编辑和扩展您的问题,您正在尝试做什么以及您的(当前)输出是什么,这将有所帮助
【解决方案2】:

我想这就是你需要的。

如果您想要 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

【讨论】:

  • 您的解决方案有助于运行所有上图和下图(包含在我上面提供的子图示例中),但我在一个子图中得到 5 个不断变化的上图,在单独的子图中得到 5 个不断变化的底部图数字。我只想要在循环内演变的子图中的 1 个上图和 1 个下图。
  • 你想在一个图中有两个子图。你总共想要多少个数字?问题不清楚
  • 是的,这就是我想要的。抱歉没有说得更清楚。我想要 5 个图形,每个图形包含 2 个子图(1 个上图和 1 个下图)。所以,差不多了。使用最后一个代码,我得到一个包含 5 个上部图像和 5 个底部图像的图形。我想分成5位数
  • 哦,顺便说一句.. a[k]b[k] 在 Matlab 中无效,所以我将它们替换为 a(k)b(k)。并将数字保存为 .png 我做了print('-dpng','-r800',sprintf('image%d.png',k))。因为saveas(f,['image.png',num2str(k)]); 出现错误。
  • 另外,我得到了图中每个子图的背景句柄。因此,我通过在子图 1 中添加 axis(handle1, 'off') 和在子图 2 中添加 axis(handle2, 'off') 来删除它。
猜你喜欢
  • 2021-11-27
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多