Export High Fidelity Graphics in MATLAB
Oct. 07, 2022
之前在使用MATLAB绘制的图像的时候,一般会采用直接复制的方式:
或者使用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
格式的图像:
将它们导入到word中,之后另存为PDF文件,可以看到它们之间细微的差别。2400 dpi的图像在细节方面的确表现得更优秀。
另外,MATLAB还提供了另外一个copygraphics
函数,用于将图片复制到粘贴板中,其参数设置和exportgraphics
函数是一致的:copygraphics - MathWorks.
Reference
[1] Save Figure with Specific Size, Resolution, or Background Color - MathWorks.