Export High Fidelity Graphics in MATLAB

Oct. 07, 2022

之前在使用MATLAB绘制的图像的时候,一般会采用直接复制的方式:

image-20221007222601167

或者使用saveas函数。

但是在R2020a以后的版本,MATLAB提供了一个更全面的函数exportgraphics用于输出图片,可以指定图片的尺寸,像素,背景色,文件格式等设置:Save Figure with Specific Size, Resolution, or Background Color - MathWorks

运行下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
clc, clear, close all

x = 1:0.1:4;
y = x.^2;

fileFormats = [".jpg?raw=true", ".tif"];
resolutions = [300, 600, 900, 1000, 2400];
for i = 1:numel(fileFormats)
    for j = 1:numel(resolutions)
        figure
        plot(x, y, 'Marker', 'o', LineWidth=1.5)
        title(strcat("Resolution: ", num2str(resolutions(j)), ", File Format: ", fileFormats(i)))
        box on
        exportgraphics(gcf, strcat("pic", num2str(resolutions(j)), fileFormats(i)), 'Resolution', resolutions(j))
    end
end

可以导出不同像素的.jpg.tif格式的图像:

image-20221007223239242

将它们导入到word中,之后另存为PDF文件,可以看到它们之间细微的差别。2400 dpi的图像在细节方面的确表现得更优秀。


另外,MATLAB还提供了另外一个copygraphics函数,用于将图片复制到粘贴板中,其参数设置和exportgraphics函数是一致的:copygraphics - MathWorks.


Reference

[1] Save Figure with Specific Size, Resolution, or Background Color - MathWorks.

[2] exportgraphics - MathWorks.

[3] copygraphics - MathWorks.