2017-03-16 67 views
0

我正在將matlab圖保存爲png。我想設置打印尺寸緊屏幕尺寸,所以我用:Matlab figure as PNG

set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size 

但是我的形象不被保存緊屏幕尺寸,也不在屏幕的中心......

這裏是我的一切,代碼我已經嘗試過:

function []=FilledCircle1(x0,y0,Radius,N,col1) 

if(N<=1) 
    error('N must be greater than 1'); 
end 

hold on 
axis equal 
% axis off 
hold on 

t=(0:N)*2*pi/N; %t=-pi:0.01:pi 
x=Radius*cos(t)+x0; 
y=Radius*sin(t)+y0; 


c1=fill(x,y,col1); 

set (c1, 'edgecolor','k') 

set(gcf,'PaperUnits','inches','PaperSize',[0.8666,0.8666],'PaperPosition',[0 0 0.8666 0.8666])%setting size (130/150, 130/150, 150pixels per inch being the default size of img), paper position is imporrtant as otherwise i will have extra border 

% % set(gcf,'Position', [0 0 4.4466 3.5733], 'units','inches') 
% % iptsetpref('ImshowBorder','tight'); 

set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size 

% set(gcf, 'Position', get(0,'screensize')); 

set(gcf,'color','none'); %set backgroound color to transparent 
fig = gcf; 
fig.InvertHardcopy = 'off'; %saves the fig with the set background color 

我想我的PNG文件看起來像這樣:

enter image description here

但是它看起來是這樣的:

enter image description here

有人可以幫助我瞭解我究竟做錯了什麼? 謝謝!

+0

第一張圖片與第二張圖片的關係並不完全清楚。無論如何,insets適用於*軸*,而不是繪製在其中。如果要繪製的對象居中放大,則必須在保存之前更改軸限制。 – excaza

+0

第一張圖片只是爲了表明我想要繪製的實心圓被居中並填滿整個屏幕。 @excaza – Mraquel

回答

3

問題是因爲x和y滴答的標籤正在替代你的圖。

enter image description here

,而不是TightInset搞亂,我只想設置axesPosition屬性使軸的內佔據整個數字

hfig = figure(); 
hax = axes(); 

t = (0:N)*2*pi/N; %t=-pi:0.01:pi 
x = Radius*cos(t)+x0; 
y = Radius*sin(t)+y0; 

c1 = fill(x,y,col1); 

set(c1, 'edgecolor','k') 

set(hfig, 'PaperUnits', 'inches', ... 
      'PaperSize', [0.8666,0.8666], ... 
      'PaperPosition', [0 0 0.8666 0.8666], ... 
      'InvertHardCopy', 'off') 

axis(hax, 'equal') 
axis(hax, 'off') 

set(hax, 'Position', [0 0 1 1]); 
set(hfig, 'Color', 'none'); 

print(hfig, '-dpng', 'output.png') 

enter image description here

你還應該考慮使用export_fig,因爲它更忠實地再現了屏幕上顯示的圖形。

+0

export_fig多次保存了我的心理健康,我也強烈建議它的使用。 – UJIN

+0

謝謝@Suever! – Mraquel