Home > voicebox > fig2pdf.m

fig2pdf

PURPOSE ^

FIG2EMF save a figure in pdf/eps/ps formats (H,S,P,F)

SYNOPSIS ^

function fig2pdf(h,s,p,f)

DESCRIPTION ^

FIG2EMF save a figure in pdf/eps/ps formats (H,S,P,F)
 [needs MikTeX installed]
 Usage:  (1) fig2pdf                       % save current figure to pdf in current folder
         (2) fig2pdf([],[],'e');           % save current figure to eps in current folder
         (3) emf=1;                        % set emf=1 to print
             figsize=[500 300];            % default size
             figdir='../figs/<m>-<n>';     % default destination
             ...
             plot (...);
             figbolden(figsize);
             if emf, fig2pdf(figdir), end

 Inputs: h   optional figure handle [use [] or omit for the current figure]
         s   file name which can include <m> for the top level
                 mfile name and <n> for figure number [use '[]' for '<m>_<n>']
                 '.' suppresses the save, if s ends in '/' or '\', then '<m>_<n>' is appended
         p   call figbolden(p) before printing the figure (use p=0 for figbolden default)
         f   output format; a combination of the following: [default 'p']
               'p'  pdf
               's'  ps
               'e'  eps

 Bugs:
    (1) MATLAB does not print the figure correctly when running under
        remote desktop; it seems to pick up the screen resolution incorrectly.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fig2pdf(h,s,p,f)
0002 %FIG2EMF save a figure in pdf/eps/ps formats (H,S,P,F)
0003 % [needs MikTeX installed]
0004 % Usage:  (1) fig2pdf                       % save current figure to pdf in current folder
0005 %         (2) fig2pdf([],[],'e');           % save current figure to eps in current folder
0006 %         (3) emf=1;                        % set emf=1 to print
0007 %             figsize=[500 300];            % default size
0008 %             figdir='../figs/<m>-<n>';     % default destination
0009 %             ...
0010 %             plot (...);
0011 %             figbolden(figsize);
0012 %             if emf, fig2pdf(figdir), end
0013 %
0014 % Inputs: h   optional figure handle [use [] or omit for the current figure]
0015 %         s   file name which can include <m> for the top level
0016 %                 mfile name and <n> for figure number [use '[]' for '<m>_<n>']
0017 %                 '.' suppresses the save, if s ends in '/' or '\', then '<m>_<n>' is appended
0018 %         p   call figbolden(p) before printing the figure (use p=0 for figbolden default)
0019 %         f   output format; a combination of the following: [default 'p']
0020 %               'p'  pdf
0021 %               's'  ps
0022 %               'e'  eps
0023 %
0024 % Bugs:
0025 %    (1) MATLAB does not print the figure correctly when running under
0026 %        remote desktop; it seems to pick up the screen resolution incorrectly.
0027 
0028 %      Copyright (C) Mike Brookes 2018
0029 %      Version: $Id: fig2pdf.m 10552 2018-06-28 13:26:36Z dmb $
0030 %
0031 %   VOICEBOX is a MATLAB toolbox for speech processing.
0032 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
0033 %
0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0035 %   This program is free software; you can redistribute it and/or modify
0036 %   it under the terms of the GNU General Public License as published by
0037 %   the Free Software Foundation; either version 2 of the License, or
0038 %   (at your option) any later version.
0039 %
0040 %   This program is distributed in the hope that it will be useful,
0041 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0042 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0043 %   GNU General Public License for more details.
0044 %
0045 %   You can obtain a copy of the GNU General Public License from
0046 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0047 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0049 switch nargin
0050     case 0
0051         h=[];
0052         s='';
0053         p=[];
0054     case 1
0055         if ischar(h) || ~numel(h)   % fig2pdf(s)
0056             s=h;
0057             h=[];
0058         else                        % fig2pdf(h)
0059             s='';
0060         end
0061         p=[];
0062         f='p';
0063     case 2
0064         if ischar(h) || ~numel(h)   % fig2pdf(s,p)
0065             p=s;
0066             s=h;
0067             h=[];
0068         else                        % fig2pdf(h,s)
0069             p=[];
0070         end
0071         f='p';
0072     case 3
0073         if ischar(h) || ~numel(h)   % fig2pdf(s,p,f)
0074             f=p;
0075             p=s;
0076             s=h;
0077             h=[];
0078         else                        % fig2pdf(h,s,p)
0079             f='p';
0080         end
0081 end
0082 if ~numel(h)
0083     h=gcf;
0084 else
0085     figure(h);
0086 end
0087 if ~numel(s)
0088     s='<m>_<n>';
0089 elseif s(end)=='/' || s(end)=='\'
0090     s=[s '<m>_<n>'];
0091 end
0092 [st,sti]=dbstack;
0093 if numel(st)>1
0094     mfn=st(end).name;  % ancestor mfile name
0095 else
0096     mfn='Figure';
0097 end
0098 if isreal(h)
0099     fn=num2str(round(h)); % get figure number
0100 else
0101     fn=num2str(get(h,'number'));  % in new versions of matlab use this method
0102 end
0103 ix=strfind(s,'<m>');
0104 while numel(ix)>0
0105     s=[s(1:ix-1) mfn s(ix+3:end)];
0106     ix=strfind(s,'<m>');
0107 end
0108 ix=strfind(s,'<n>');
0109 while numel(ix)>0
0110     s=[s(1:ix-1) fn s(ix+3:end)];
0111     ix=strfind(s,'<n>');
0112 end
0113 if numel(p)>0
0114     if numel(p)==1 && p==0
0115         figbolden;
0116     else
0117         figbolden(p)
0118     end
0119 end
0120 set(gcf,'paperpositionmode','auto');    % preserve screen shape
0121 if ~strcmp(s,'.')
0122     if isempty(f)
0123         f='p'; % default is pdf
0124     end
0125     sp=[s  '.pdf'];
0126     print('-dpdf',sp);
0127     %     set(gcf,'PaperPosition',[0.6350 6.3500 20.3200 12]);
0128     system(['pdfcrop ' sp ' ' sp]); % needs MikTeX installed
0129     if any(f=='s')
0130         system(['pdf2ps ' sp ' ' s '.ps']);
0131     end
0132     if any(f=='e')
0133         system(['pdf2ps ' sp ' ' s '.eps']);
0134     end
0135     if ~any(f=='p')
0136         system(['del ' sp]);
0137     end
0138 end

Generated on Mon 06-Aug-2018 14:48:32 by m2html © 2003