Reputation: 245
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
Upvotes: 7
Views: 21578
Reputation: 791
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.
LaTeX
code which includes the figuresPDFLaTeX
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.
Upvotes: 3
Reputation: 106
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
Upvotes: 0
Reputation: 269
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.
Upvotes: 3
Reputation: 18530
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));
Upvotes: 3
Reputation: 56
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/
Upvotes: 4