问题
Is there any way to do this?
I know how to use saveas (saveas(1, filename, 'pdf');
) to save one figure to a PDF file, but is it possible to add multiples? So something like (saveas(1,2,3) filename, 'pdf'));
.
Thanks
回答1:
I don't think so - you need to increment the file name in some manner. I would use something like:
for ii=1:3
saveas(ii,[filename '-' num2str(ii)],'pdf)
end
As a side note, I have had repeated difficulties when including the pdfs generated by matlab in a manuscript submission. My current solution is to produce eps files and convert with a shell script.
r/
回答2:
I thought it might be worth pointing out that the behavior you are aiming for can be obtained using hgsave
and hgload
, BUT only if you are happy to save using .fig. The documentation for these functions fooled me for a while into believing they could work with other extensions (such as .pdf), but I couldn't get an example to work on my machine (Linux Mint v12, Matlab r2012b). Perhaps someone else might be able to do better. An example of it working with the .fig extension follows:
%# 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));
回答3:
Late reply, but I thought I'd add that you could use the publish command and publish to pdf. Create an m-file 'myfile.m' with the plot commands, as in
plot(x1,y1);
plot(x2,y2);
Then run this file using
publish('myfile.m', 'pdf')
This should give you what you want.
回答4:
The MATLAB-PDFLaTeX solution
Using MATLAB's publish
command is a great solution, as other answers have pointed out. If you are looking for more control in terms of how the different figures are combined, another solution is to use pdflatex
to compile the figures into a single PDF.
- Print the figures to PDF
- Generate
LaTeX
code which includes the figures - Compile with
PDFLaTeX
Below is a proof of concept which takes a file name as a char
and some number of function_handle
s and produces a PDF which contains those figures.
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
Example:
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.
回答5:
There is no inbuild command to save all figure in one pdf, there are many workaround tough
Create pdf files for each figure and combine them using software which are easily available.
There is script called Export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629) which can save figures in single pdf
- Save all figures in .ps format and convert .ps into PDF (I find quite easy to use). fname = 'test.ps'; print ( '-dpsc2', fname, '-f1' ) print ( '-dpsc2', fname, '-append', '-f2' )
来源:https://stackoverflow.com/questions/14188572/matlab-saving-multiple-figures-to-a-pdf