有没有办法做到这一点?
我知道如何使用saveas(saveas(1,filename,'pdf');
)将一个图保存为PDF文件,但是否可能添加多个?所以类似于(saveas(1,2,3)filename,'pdf'
);。
谢谢
有没有办法做到这一点?
我知道如何使用saveas(saveas(1,filename,'pdf');
)将一个图保存为PDF文件,但是否可能添加多个?所以类似于(saveas(1,2,3)filename,'pdf'
);。
谢谢
我认为你需要以某种方式增加文件名的编号。我建议使用类似以下格式的命名:
for ii=1:3
saveas(ii,[filename '-' num2str(ii)],'pdf)
end
hgsave
将多个图形保存到一个文件中。然而,据我所知,它仅适用于.fig扩展名。我已经添加了一个带有示例代码的答案,指出如何完成此操作。 - Colin T Bowers使用MATLAB的 publish
命令是一个很好的解决方案,正如其他答案所指出的那样。如果你想在不同图形的组合方面获得更多控制权,另一种解决方案是使用 pdflatex
将图像编译成单个PDF。
LaTeX
代码PDFLaTeX
编译下面是一个概念验证,它接受文件名作为 char
和一些 function_handle
数字,并生成一个包含这些图形的 PDF。
function res = save2pdf(name,varargin)
pathToPdflatex = '/Library/TeX/texbin/pdflatex' ;
files = cell(size(varargin)) ;
for ii = 1:numel(varargin)
files{ii} = sprintf('%s_fig%g.pdf',name,ii) ;
print(varargin{ii},'-dpdf','-painters',files{ii}) ;
end
fh = fopen(sprintf('%s.tex',name),'w+') ;
fprintf(fh,'\\documentclass{article}\n') ;
fprintf(fh,'\\usepackage{graphicx}\n') ;
fprintf(fh,'\\begin{document}\n') ;
for ii = 1:numel(files)
fprintf(fh,'\\includegraphics[width=\\textwidth]{%s}\n\\newpage\n',files{ii}) ;
end
fprintf(fh,'\\end{document}\n') ;
fclose(fh) ;
[~,res] = system(sprintf('%s %s.tex',pathToPdflatex,name)) ;
disp(res)
end
例子:
n = 1e+5 ;
x0 = cumsum(randn(n,1)) ;
x1 = cumsum(randn(n,1)) ;
f0 = figure() ;
f1 = figure() ;
ax0 = axes('Parent',f0) ;
ax1 = axes('Parent',f1) ;
plot(ax0,x0) ;
plot(ax1,x1) ;
save2pdf('my_figures',f0,f1)
voila:
...
Output written on my_figures.pdf (2 pages, 169718 bytes).
Transcript written on my_figures.log.
hgsave
和 hgload
实现,但前提是您愿意使用 .fig 进行保存。这些函数的文档曾经误导了我一段时间,让我相信它们可以使用其他扩展名(例如 .pdf),但我无法在我的机器上(Linux Mint v12,Matlab r2012b)使示例正常工作。也许其他人能够做得更好。下面是一个使用 .fig 扩展名运行的示例:%# Create some example data
x = (0:10)';
y1 = (1/10) * x;
y2 = sin(x);
%# Create an array of figures and an array of axes
AllFig(1) = figure('Visible', 'off');
AllFig(2) = figure('Visible', 'off');
AllAxes(1) = axes('Parent', AllFig(1));
AllAxes(2) = axes('Parent', AllFig(2));
%# Plot the data on the appropriate axes
plot(AllAxes(1), y1);
plot(AllAxes(2), y2);
%# Save both figures to .fig in one hit using hgsave
hgsave(AllFig, 'TwoFigsOneFile.fig');
% Clear the workspace
clear
%# Load both figures in one hit using hgload
LoadFig = hgload('TwoFigsOneFile.fig');
%# Display the first figure and second figure
figure(LoadFig(1));
figure(LoadFig(2));
plot(x1,y1);
plot(x2,y2);
然后使用以下命令运行该文件:
publish('myfile.m', 'pdf')
没有内置命令可以将所有图形保存在一个PDF中,但有许多解决方法。
为每个图形创建PDF文件,并使用易于获得的软件将它们组合起来。
有一个名为Export_fig(http://www.mathworks.com/matlabcentral/fileexchange/23629)的脚本,可以将图形保存在单个PDF中。
hgsave
来实现你想要的内容。 - Colin T Bowershgsave
只能使用 .fig 扩展名工作。根据文档,它看起来可能可以使用其他文件类型,但在实际操作中并不行(至少在我的机器上 - 运行 R2012b 的 Linux Mint)。 - Colin T Bowers