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

前端 未结 3 1086
[愿得一人]
[愿得一人] 2020-12-30 00:45

In a MATLAB function I am writing, I am generating a figure. The figure is displayed when the function is executed. I need to save the figure as a JPEG image. To do that, I

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 01:03

    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)

    image created with export_fig

    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
    

提交回复
热议问题