To export MATLAB figures as high-resolution PNG images, you can use built-in functions like print or exportgraphics

To export MATLAB figures as high-resolution PNG images, you can use built-in functions like print or exportgraphics (available in newer MATLAB versions). Below are the most effective methods:


1. Using print Function (Recommended for Full Control)

Adjust the resolution (DPI) and figure size to ensure high quality.

Steps:

  1. Set the figure size and properties:
    matlab
    Copy
    fig = figure; % Create or reference your figure
    set(fig, 'Position', [100, 100, 800, 600]); % Adjust figure size [x, y, width, height]
    set(fig, 'Color', 'w'); % Set background to white (optional)
    plot(1:10, rand(1,10)); % Your plot
  2. Export with high resolution (e.g., 300 DPI):
    matlab
    Copy
    print(fig, '-dpng', '-r300', 'high_res_image.png'); % -r300 sets 300 DPI
    • Replace high_res_image.png with your filename.
    • Use -r600 for 600 DPI (higher resolution).

2. Using exportgraphics (R2020a+)

Simpler syntax with built-in resolution and background control:

matlab
Copy
exportgraphics(fig, 'high_res_image.png', 'Resolution', 300);
  • Resolution sets the DPI (default is 150).
  • Use 'BackgroundColor','white' to avoid transparent backgrounds.

3. Using saveas (Basic Method, Limited Control)

Less flexible but quick:

matlab
Copy
saveas(fig, 'image.png'); % Default resolution (screen resolution)
  • For higher quality, combine with print settings.

4. Export Setup GUI (Interactive)

  1. Open the figure.
  2. Go to File > Export Setup.
  3. In the Export Setup window:
    • Set Size (increase width/height for higher resolution).
    • Under Rendering, set Resolution (DPI) (e.g., 300).
    • Check “Custom Size” to maintain aspect ratio.
  4. Click Export and save as PNG.

Key Tips for High Quality

  • Increase DPI: Use -r300 or higher in print.
  • Antialiasing: Enable figure smoothing for crisp lines/text:
    matlab
    Copy
    set(fig, 'GraphicsSmoothing', 'on');
    set(gca, 'FontSmoothing', 'on');
  • Vector vs. Raster: For vector-based plots, use -dpdf or -dsvg instead of PNG.
  • Renderers: Use -painters (vector) or -opengl (raster) in print:
    matlab
    Copy
    print(fig, '-dpng', '-opengl', '-r300', 'image.png');

Troubleshooting

  • Blurry Text: Increase DPI and use exportgraphics or print with -opengl.
  • Transparent Background: Use exportgraphics with 'BackgroundColor','none'.
  • Large File Size: Reduce DPI or use imwrite with compressed PNG options.

Example Workflow

matlab
Copy
% Create a figure
fig = figure;
plot(1:10, rand(1,10), 'LineWidth', 2);
set(fig, 'Position', [100, 100, 1000, 800]); % Larger figure size
set(gca, 'FontSize', 12); % Increase font size

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

% Or use exportgraphics (R2020a+)
% exportgraphics(fig, 'high_res_plot.png', 'Resolution', 300);

Summary

  • Best Quality: Use print with -r300 (or higher) and adjust figure size.
  • Simplicity: Use exportgraphics in newer MATLAB versions.
  • Avoid saveas for high-resolution needs—it uses screen resolution by default.