2015-11-18 23 views
2

我试图用无默认维度保存图形,但我总是获取默认大小图。Matlab不保存绘图的新维度

figure 
    for i=1:6 
     for j=1:4 
      subplot(6,4,j+(i-1)*4) 
      [...] 
     end 
    end  
    w=600; h=800; 
    set(gcf,'Position',[30 60 w h]); 
    set(gcf, 'PaperUnits', 'centimeters'); 
    set(gcf,'PaperPositionMode','manual') 
    print('test','-depsc'); 

它主要是一个6乘4的子图。我在互联网上寻找解决方案,我发现了许多关于Position,PaperUnits,PaperPositionMode的评论,但目前还没有任何评论。唯一可行的解​​决方案是当我以.fig格式导出时。

我真正意识到的是,在set(gcf,'Position',[30 60 w h])之后,图的窗口尺寸是正确的,但是当尺寸是默认尺寸时,子图被挤压。如果我只插入一个pause并手动调整窗口大小(即使几乎没有),子图可以在较大的窗口内很好地展开。但是,对于print命令,结果并不是我们想要的结果。

我也试过saveas,我得到了同样的结果。

啊,如果我手动保存图,它的工作原理是完美的,但是,当然,我需要自动化过程!

感谢您的帮助!

+0

在一般情况下,如果你想Matlab来正确保存的东西,使用FEX提交'export_fig' –

+0

这就是说,'paperUnits'是图选项中的纸张打印时。 –

+0

更改图形的'位置'属性只会改变图形本身的大小(即只包围图形的窗口)。要改变你的情节大小,你必须循环你的形象的孩子,更精确地说,在'轴'类型的孩子。这正是Matlab在“手动”调整图形大小时所做的工作 – BillBokeey

回答

0

虽然我不能重现此行为MATLAB 2014B自动处理轴调整,我还是会发布一个答案,这可能需要一些调整。

基本上,你需要做的是:

% Get a handle to your figure object 
Myfig=gcf; 

% Get all of your figure Children - Warning : if you have a global 
% legend in your figure, it will belong to your figure Children and you 
% ll have to ignore it 
AllChildren=get(Myfig,'Children'); 
MyAxes=findobj(AllChildren,'type','axes'); 

% Now you have your axes objects, whose Position attributes should be in 
% normalized units. What you need to do now is to store these positions 
% before changing the size of your figure, to reapply them after 
% resizing (the beauty of normalized positions). 
Positions=zeros(size(MyAxes,1),4); 

for ii=1:size(MyAxes,1) 

    Positions(ii,:)=get(MyAxes(ii),'Position'); 

end 

% Resize your figure 
w=600; 
h=800; 
set(gcf,'Position',[30 60 w h]); 

% Reuse the positions you stored to resize your subplots 
for ii=1:size(MyAxes,1) 

    set(MyAxes(ii),'Position',Positions(ii,:)); 

end 

这应该做的伎俩。