How to save MATLAB figure as JPEG using saveas() without the image coming off badly?

流过昼夜 提交于 2019-11-30 06:03:22

The Matlab figure export dialog and the saveas() function lack a lot of desirable functionality. Especially, savas() cannot create a custom resoultion image which is why your results look poor. For creation of bitmap images I highly recommend using the third-party function export_fig. By adding the following code to your function (including the maximizing trick)

set(gcf, 'Color', 'white'); % white bckgr
export_fig( gcf, ...      % figure handle
    'Export_fig_demo',... % name of output file without extension
    '-painters', ...      % renderer
    '-jpg', ...           % file format
    '-r72' );             % resolution in dpi

I created this image: (use "show image" or similar in your browser to get the original size)

For higher quality use higher resolutions of 150 or even 300 dpi (for print). Instead of maximizing the figure window, for most applications it's suitable to define the axis size to obtain an image of the desired size:

unitSave = get(figureHandle, 'Unit');                % store original unit
set(figureHandle, 'Unit', 'centimeters');            % set unit to cm
set(figureHandle,'position',[0 0 width height]);     % set size
set(figureHandle, 'Unit', unitSave);                 % restore original unit

Just use a lossless scalable format like EPS, see last line in the snippet below :)

h1=figure % create figure
plot(t,Data,'r');
legend('Myfunction');

% Create title with required font size
title({'Variance vs distance'},'LineWidth',4,'FontSize',18,...
'FontName','Droid Sans');

% Create xlabel with required font size
xlabel({'Distance (cm)'},'FontSize',14,...
'FontName','DejaVu Sans');

% Create ylabel with required font size
ylabel({'Variance of sobel gradients'},'FontSize',14,...
'FontName','DejaVu Sans');

print(h1,'-depsc','autofocus.eps') % print figure to a file

I cannot attach an EPS file here though, not supported, but its scalable and can be put in word processors or Latex without worrying about bad resolution.

I had the same problem, and here is what I used to solve it:

set(gcf,'PaperPositionMode','auto') saveas(gcf,'file_to_save','png')

where gcf can be replaced by the handle to the desired figure.

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