Step-by-Step Guide to Export MATLAB Figures as High-Resolution PNG Files

Including code examples for each method would help. For instance, using ‘print’ with ‘-dpng’ and ‘-r300’, and ‘exportgraphics’ with ‘Resolution’ set to 300. Highlighting that ‘exportgraphics’ is available from R2020a onwards is important for compatibility.

I

Use these methods to save crisp, publication-quality images directly from MATLAB figures.


Method 1: Use exportgraphics (Recommended for R2020a+)

The modern exportgraphics function provides precise control over resolution and cropping.

matlab
Copy
% Create a figure
figure;
plot(1:10, rand(1,10), 'LineWidth', 2);
title('Sample Plot');

% Export as 300 DPI PNG with minimal whitespace
exportgraphics(gcf, 'high_res_plot.png', ...
    'Resolution', 300, ...          % Set DPI (default: 150)
    'ContentType', 'auto', ...      % Retain vector/raster elements
    'BackgroundColor', 'white');    % Prevent transparent background

Key Options:

  • Resolution: 300-600 DPI for publications.
  • ContentType: Use 'vector' for line plots, 'image' for pixel-based graphics.
  • BackgroundColor: Override default transparency.

Method 2: Use print (Legacy Approach)

The print function offers compatibility with older MATLAB versions.

matlab
Copy
% Adjust figure properties
fig = gcf;
fig.Units = 'inches';               % Set units for accurate sizing
fig.Position = [0 0 6 4];           % Width=6", Height=4" (aspect ratio)
fig.Renderer = 'painters';          % Use vector rendering for sharp lines

% Export as 600 DPI PNG
print(fig, 'high_res_plot.png', '-dpng', '-r600');

Common Flags:

  • -dpng: Output format.
  • -r600: Resolution (600 DPI).
  • -opengl/-painters: Renderer (use -painters for vector graphics).

Method 3: Remove Whitespace Programmatically

Eliminate unwanted padding around the figure.

matlab
Copy
% Adjust axes to fill the figure
ax = gca;
ax.Position = [0.1 0.1 0.85 0.85];  % Reduce margins (left, bottom, width, height)

% Export with tight bounding box
exportgraphics(gcf, 'no_whitespace_plot.png', ...
    'Resolution', 300, ...
    'ContentType', 'vector');

Advanced Tips

  1. Vector vs. Raster:
    • Use 'ContentType','vector' for line plots (scalable, smaller file size).
    • Use 'ContentType','image' for heatmaps/surfaces (prevants aliasing).
  2. Anti-Aliasing:
    matlab
    Copy
    set(gcf, 'GraphicsSmoothing', 'on'); % Smooths jagged lines
  3. Batch Export:
    matlab
    Copy
    % Save all open figures
    figs = findall(groot, 'Type', 'figure');
    for i = 1:length(figs)
        exportgraphics(figs(i), sprintf('plot_%d.png', i), 'Resolution', 300);
    end
  4. CMYK for Print:
    Convert RGB to CMYK using print:

    matlab
    Copy
    print(fig, '-dpng', '-r600', '-cmyk', 'print_ready_plot.png');

Comparison of Methods

Method Pros Cons
exportgraphics Easy, minimal code, auto-cropping Requires MATLAB R2020a+
print Compatible with older versions Manual whitespace removal needed
saveas (Avoid) Simplest syntax Low resolution, no DPI control

Troubleshooting