Skip to content

rs_save_figs(fullname, whichfigs, opts)

rs_save_figs(whichfigs,whichfigs,opts) writes one or more open figures to a fig file

Input arguments:

  • fullname (char) –

    file name, with path; if no figure handle is given, then all figures are saved, and '_fig_n' is appended, where n is sequentially incremented

  • whichfigs (handle or array of handles or char) –

    a figure handle, a list of handles, or 'all', or empty, or omitted; if empty, current figure; default is [] (empty)

  • opts (struct) –

    options, can be omitted, with fields

    • if_log (int) – 1 to log, 0 to omit; default is 0
    • ndigits (int) – number of digits to use in file name; default is 0, which uses minimum number for available figures

See also: zpad, rs_save_mat.

Source code in src/rs_save_figs.m
function rs_save_figs(fullname,whichfigs,opts)
% rs_save_figs(whichfigs,whichfigs,opts) writes one or more open figures to a fig file
%
% Args:
%   fullname (char): file name, with path; if no figure handle is given, then all figures are saved, and '_fig_n' is appended, where n is sequentially incremented
%   whichfigs (handle or array of handles or char): a figure handle, a list of handles, or 'all', or empty, or omitted; if empty, current figure; default is [] (empty)
%   opts (struct): options, can be omitted, with fields
%
%      - if_log (int): 1 to log, 0 to omit; default is 0
%      - ndigits (int): number of digits to use in file name; default is 0, which uses minimum number for available figures
%
% See also:  ZPAD, RS_SAVE_MAT.
%
fullname=strrep(fullname,'.fig',''); %strip .fig if supplied
if nargin<=1
    whichfigs=[];
end
if nargin<=2
    opts=struct;
end
opts=filldefault(opts,'if_log',0);
opts=filldefault(opts,'ndigits',0);
%
if isempty(whichfigs)
    handles=gcf;
    if_append=0;
elseif ischar(whichfigs)
    handles=flipud(findobj('Type','figure'));
    if_append=1;
else
    handles=whichfigs;
    if length(handles)>=2
        if_append=1;
    else
        if_append=0;
    end
end
if length(handles)>0
    if opts.ndigits<=0
        opts.ndigits=floor(log10(length(handles)))+1;
    end
    for ifig=1:length(handles)
        figname=get(handles(ifig),'Name');
        if (if_append)
            savename=cat(2,fullname,'_fig_',zpad(ifig,opts.ndigits));
        else
            savename=fullname;
        end
        savename=cat(2,savename,'.fig');
        savefig(handles(ifig),savename);
        if opts.if_log
            disp(sprintf('figure %s saved as %s',figname,savename));
        end
    end
end
return
end