How do I save multiple figures to a single PDF file in MATLAB?

I have a figure array (6x1) of 6 figures with handle, 'k' in MATLAB R2020a. I can type 'saveas(k,'test.fig')' and the six figures are saved to a single MATLAB file. I would like to save the six figures to a single PDF or TIF file, both of which support multiple pages. If I use 'saveas(k,'test.pdf')' or 'saveas(k,'test.tif')', I get the errors:
Error using checkArgsForHandleToPrint
Handle input must be scalar, vector, or cell array of vectors.
Error in checkArgsForHandleToPrint
Error in print>LocalCreatePrintJob (line 101)
handles = checkArgsForHandleToPrint(0, varargin{:});
Error in print (line 38)
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
Error in saveas (line 181)
print( h, name, ['-d' dev{i}] )
Is there a way I can save the multiple figures in a figure array to a single PDF or TIF file? Is there a built in MATLAB function with arguments that can do this?

 Accepted Answer

The reason you have an issue with the handle is because the figures are closed. The following script creates a 1x6 figure array 'k' and saves the figures to a pdf using the 'exportgraphics' function. Note that all the figures need to be open before you can save them.
k = repmat(figure, 1, 6);
filename = "figures.pdf";
for i = 1:length(k)
exportgraphics(k(i), filename, 'append', true);
end
close all

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!