Search code examples
matlabmatlab-figure

Saving multiple figures all at once


I need to save all the figures that generated from one set of MATLAB code into one single pdf file instead having them in separate files.

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');

Solution

  • Depending on your exact needs you could do something like,

    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
    

    You can add more axes and change their size by setting the properties of ax1 and ax2.