【发布时间】:2013-10-05 19:28:40
【问题描述】:
在for 循环中,我创建了可变数量的子图,这些子图显示在一个图形上。我还可以将每个子图另存为单独的全尺寸图和图像文件(最好是 JPG)吗?
【问题讨论】:
标签: matlab plot subplot handles save-as
在for 循环中,我创建了可变数量的子图,这些子图显示在一个图形上。我还可以将每个子图另存为单独的全尺寸图和图像文件(最好是 JPG)吗?
【问题讨论】:
标签: matlab plot subplot handles save-as
假设你有subfigure的轴的句柄ha,你可以使用getframe和frame2im如下,
F = getframe(ha);
[im,map] = frame2im(F);
if isempty(map)
imwrite(im,'subframe.jpg');
else
imwrite(im,map,'subframe.jpg');
end
请注意,这将准确地保存轴在屏幕上的显示方式,因此请在保存之前根据自己的喜好调整图形大小。要尽可能多地使用图形空间,请在 MATLAB Central 上试用 subfigure_tight 函数。
【讨论】:
使用 copyobj 到一个新图形,然后使用带有新图形句柄的 saveas:
您应该提供的示例代码(请参阅SSCCE):
figure
nLines = 2;
nColumns = 3;
handles = zeros(nLines,nColumns)
for line = 1:nLines
for column = 1:nColumns
handles(line,column)=subplot(nLines,nColumns,column+(line-1)*nColumns);
plot([line column]);
title(sprintf('Cool title (%d,%d)',line,column))
ylabel(sprintf('Ylabel yeah (%d,%d)',line,column))
xlabel(sprintf('Xlabel nah (%d,%d)',line,column))
end
end
这里我保存了子图句柄,但假设您没有保存它们:
axesH = findobj(gcf,'Type','axes','-not','Tag','legend'); % This will change the order from which the axes will be saved. If you need to save in the correct order, you will need access to the subplot handles
nAxes = numel(axesH)
newFig = figure;
for k=1:nAxes
newAxes=copyobj(axesH(k),newFig);
% Expand subplot to occupy the hole figure:
set(newAxes,'OuterPosition',[0 0 1 1]);
tightInset=get(newAxes,'TightInset');
set(newAxes,'Position',[tightInset(1:2) [1 1]-(tightInset(3:4)+tightInset(1:2))])
saveas(newFig,sprintf('axes_%d.jpg',k),'jpg');
delete(newAxes);
end
delete(newFig);
保存一个轴的示例:
为了删除死区,我使用了可用的信息on this topic。
【讨论】: