一次保存多个数字

Saving multiple figures all at once

我需要将一组 MATLAB 代码生成的所有图形保存到一个 pdf 文件中,而不是将它们放在单独的文件中。

X = rand(20,1);
Y = rand(20,1);
t=(1:20)';
figure(1);
plot(t,X);
saveas(gcf,'figure1.pdf');
figure(2);
plot(t,Y);
saveas(gcf,'figure2.pdf');

根据您的具体需要,您可以做类似的事情,

close all
X = rand(20,1);
Y = rand(20,1);
t=(1:20)';

% Create a figure with enough room for two axes:
figure('Position',[600 400 600 800]);

ax1 = axes('Position',[0.1 0.06 0.85 0.43]); % Set axes 1 properties
plot(t,X); % Plot data 1
xlabel('x1'); ylabel('y1');
set(gca,'fontsize',14)

ax2 = axes('Position',[0.1 0.55 0.85 0.43]); % Set axes 2 properties
plot(t,Y); % Plot data 2
xlabel('x2'); ylabel('y2');
set(gca,'fontsize',14)

print -dpdf -bestfit Figure.pdf % Print figure to pdf with -bestfit property

您可以通过设置 ax1ax2 的属性来添加更多轴并更改它们的大小。

如果这个问题出现在您的搜索结果中,MATLAB 版本高于 R2020a 可以将多个图形保存在一个 PDF 文件中,具有以下内容 command and switch:

exportgraphics(ax,'myplots.pdf','Append',true)