MATLAB only prints a part of my figure

断了今生、忘了曾经 提交于 2020-01-16 02:03:41

问题


I'm trying to print my figure in MATLAB, but it keeps screwing up and I have no idea why.

opslaan = figure(1);
        plot(1:handles.aantal,handles.nauw,'-r','LineWidth',1.5);
        xlabel(gca,sprintf('Framenummer (%g ms per frame)',60/handles.aantal));
        ylabel(gca,'dB');
        set(gca,'YGrid','on');
        yAsMax = ceil( ceil(max(handles.nauw)) / 2) * 2;
        axis([0 handles.aantal 0 yAsMax]);
        pause(1);
        print -dpng image.png 

The first line is just plotting the data on my figure, then labeling x and y, turning on grid and calculating the y-axis like I want it. This all works great and MATLAB shows it like I want it in the figure window. When saving to .png / .jpeg / .eps, it goes wrong and only prints the bottom left corner (473x355 pixels), the rest just disappeared.

When exporting manually via File -> Save As, it works correctly.

How do I fix it?


回答1:


Try using the following line instead of the print line you already have.

print(opslaan, '-dpng', 'image.png')

Another option would be to look into imwrite.




回答2:


The post Making Pretty Graphs contains an excellent tutorial about how to generate publication ready images from MATLAB figures.

In general, printing figures to image files programmatically should contain the following steps.

% Some data to be plotted
t1 = 0:0.1:4*pi ;
t2 = 0:0.01:4*pi ;
f1 = sin(t1) + randn(size(t1))/10 ;
f2 = sin(t2) + cos(t2.^2)/10 + randn(size(t2))/100 ;

% Define the styles to be used
linstyle_11 = {'LineStyle','--','Color',[.3 .6 .6],'LineWidth',1} ;
linstyle_12 = {'LineStyle','o','Color',[.3 .6 .6],'LineWidth',2,'MarkerSize',8,'MarkerFaceColor','m'} ;
linstyle_2 = {'Color',[.6 .3 .6],'LineWidth',3} ;
titleprops = {'FontSize',24,'FontWeight','bold',...
    'FontName','FixedWidth','FontAngle','oblique',...
    'Interpreter','None'} ;
axisprops = {'FontSize',20,'FontWeight','normal','FontName','FixedWidth'} ;
ylim_s = [-2 2] ;

% Set up the figure for PNG/JPEG export
canvas = [1 1 2048 1024] ;
fh = figure(1);
set( fh, ...
    'PaperPositionMode','auto', ...
    'Position',canvas ) ;

% Do the plotting and apply styles
ha1 = subplot(2,1,1);
hp11 = plot(t1,f1);
hold on
hp12 = plot(t1,f1);
hold off
title('subplot 1',titleprops{:})
xlabel('t',titleprops{:})
ylabel('f1',titleprops{:})

ha2 = subplot(2,1,2);
hp2 = plot(t2,f2);
title('subplot 2',titleprops{:})
xlabel('t',titleprops{:})
ylabel('f2',titleprops{:})

% finish appliing styles
set(hp11,linstyle_11{:}) ;
set(hp12,linstyle_12{:}) ;
set(hp2,linstyle_2{:}) ;
set(ha1,axisprops{:}) ;
set(ha2,axisprops{:}) ;

% Print
outfname = 'test' ;
print(fh,'-dpng',[outfname,'.png']) ;

% Sometimes it's better to use vector graphics ==>
% alter figure properties to allow proper PDF printouts.
set( fh, ...
        'PaperSize', [29.71 13.01], ...
        'PaperPosition', [0.01 0.01 29.7 13.0] ) ;

% print to pdf file
print(fh,'-dpdf',[outfname,'.pdf']) ;


来源:https://stackoverflow.com/questions/11341222/matlab-only-prints-a-part-of-my-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!