Code covered by the BSD License  

Highlights from
save_figure

image thumbnail

save_figure

by

 

08 Mar 2012 (Updated )

Saves one or multiple open figures with specified filename[-prefix] and format.

save_figure(varargin)
%   Saves current, all, or specified figures with specified filename[-prefix]
%   and format[, numbered according to figure-handle].
%
%   Syntax:
%              save_figure()
%              save_figure(fn)
%              save_figure(fn, fmt)
%              save_figure(fn_prefix, fmt, 'all')
%              save_figure(fn_prefix, fmt, handles)
%
%   where
%               'fn' is the filename (without extension)
%              'fmt' is the output format ('eps', 'pdf', or 'png')
%        'fn_prefix' is the filename-prefix
%          'handles' is a vector of figure-handles to save
%
%   Examples: 
%              >> save_figure()
%              the current figure is saved as 'fig.eps'
%
%              >> save_figure('foo')
%              the current figure is saved as 'foo.eps'
%
%              >> save_figure('bar', 'png')
%              the current figure is saved as 'bar.png' (300 DPI)
%
%              >> save_figure('fig_', 'eps', 'all')
%              all figures are saved as EPS-files named 
%              "fig_<figure handle>.eps" (e.g. "foo_01.eps").
%
%              >> save_figure('fig_', 'pdf', [1 2 7])
%              figures 1, 2, and 7 are saved as PDF-files named 
%              "fig_<figure handle>.pdf" (e.g. "fig_07.pdf").
%
%   Note:      
%              If fmt=='pdf', for better layout, every figure is first saved in
%              EPS-format. The exported EPS-file is then converted to PDF-format
%              using the ps2pdf utility (Ghostscript package). Thereafter, the
%              exported EPS-file is removed.
%
%              If fmt=='png', for better layout, every figure saved in
%              EPS-format, converted to PDF-format (see above), and then
%              rasterised using the convert utility (Imagemagick or 
%              Graphicsmagick package). Thereafter, temporary files are removed.
%
%   Requirements:
%              ps2pdf  utility (Ghostscript package)  [for saving PDFs and PNGs]
%              convert utility (Imagemagick or Graphicsmagick package) 
%                                                              [for saving PNGs]
%
%   Copyright Christoph Feenders, 2012


function save_figure(varargin)

    DEFAULT_FN  = 'fig';
    DEFAULT_FMT = 'eps';

    switch nargin
        case 0
            fn  = DEFAULT_FN;
            fmt = DEFAULT_FMT;
        case 1
            fn  = varargin{1};
            fmt = DEFAULT_FMT;
        case 2
            fn  = varargin{1};
            fmt = varargin{2};
        case 3
            fn_prefix = varargin{1};
            fmt       = varargin{2};
            if ischar(varargin{3})
                if strcmp(varargin{3}, 'all')
                    figs = findobj('Type', 'figure')';  % determine figure handles                
                else
                    error('argument "%s" invalid', varargin{3})
                end
            else
                figs = varargin{3};
                if size(figs, 1) > 1
                    figs = figs';
                end
            end
        otherwise
            error('too many arguments')
    end
    
    if exist('fn', 'var')
        save_a_figure(fn, fmt)
    else
        % loop over figure handles without changing the handles' order
        for k = figs(end:-1:1)

            figure(k)                             % set current figure
            fn = sprintf('%s%02i', fn_prefix, k); % set figure file-name
            save_a_figure(fn, fmt)                % save figure

        end
    end
  
end


%   Saves current figure with specificied filename and format.
%
%   Syntax:
%              save_a_figure(fn, fmt)
%
%   where
%               'fn' is the filename (without extension)
%              'fmt' is the output format (eps, pdf, png)
%
%   Example: save_a_figure('foo', 'pdf') saves the current figure as EPS in
%            file "foo.tmp", converts it to a PDF stored as "foo.pdf",
%            and deletes "foo.tmp". For PNG-files, first a PDF-file is generated
%            (as described before), which is then converted to PNG-format.
%
%   Copyright Christoph Feenders, 2012

function save_a_figure(fn, fmt)

    DPI_PNG = 300; % resolution for raster graphics

    % avoid cutting off plot-edges
    set(gcf, 'PaperPositionMode', 'auto')

    % determine dimensions of figure
    rect = get(gcf, 'Position'); % rect = [left, bottom, width, height]
    if rect(3) > rect(4)
        set(gcf, 'PaperOrientation', 'landscape')
    end

    % save [and convert] figure    
    print('-depsc2', [fn '.tmp']);  % save as EPS-color level 2
    switch fmt
        case 'eps'  % rename file
            system(sprintf('mv "%s.tmp" "%s.eps"', fn, fn));  
        case 'pdf'  % convert EPS to PDF and remove temporary EPS
            eval(sprintf('!ps2pdf -dEPSCrop "%s.tmp" "%s.pdf"', fn, fn)) 
            system(sprintf('rm "%s.tmp"', fn));
        case 'png'  % convert EPS to PDF, convert PDF to PNG, remove temporary PDF
            eval(sprintf('!ps2pdf -dEPSCrop "%s.tmp" "%s.tmp2"', fn, fn))
            system(sprintf('mv "%s.tmp2" "%s.tmp"', fn, fn));
            eval(sprintf('!convert -density %i "%s.tmp" "%s.png"', DPI_PNG, fn, fn))
            system(sprintf('rm "%s.tmp"', fn));
        otherwise
            error(['Figure not saved: format "' fmt '" unknown.'])
    end

end

Contact us