2015-11-18 84 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 

這應該做的伎倆。