# v_fig2pdf

## PURPOSE

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

## SYNOPSIS

function v_fig2pdf(h,s,p,f)

## DESCRIPTION

    V_FIG2EMF save a figure in pdf/eps/ps formats (H,S,P,F)
     [needs MikTeX installed]
     Usage:  (1) v_fig2pdf                       % save current figure to pdf in current folder
             (2) v_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 (...);
                 v_figbolden(figsize);
                 if emf, v_fig2pdf(figdir), end

     Inputs: h   optional figure handle [Use [] or omit or gcf for the current figure]
             s   file name which can include <m> for the top level mfile name and <n>
                 for the figure number. If s ends in '/' or '\', then '<m>_<n>' is appended.
                 Use s='.' to suppress the save. [Use '[]' for s='<m>_<n>']
             p   call v_figbolden(p) before printing the figure (use p=0 for v_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:

- v_figbolden V_FIGBOLDEN embolden, resize and recolour the current figure =(POS,PV,M)

This function is called by:

## SOURCE CODE

    0001 function v_fig2pdf(h,s,p,f)
    0002 %V_FIG2EMF save a figure in pdf/eps/ps formats (H,S,P,F)
    0003 % [needs MikTeX installed]
    0004 % Usage:  (1) v_fig2pdf                       % save current figure to pdf in current folder
    0005 %         (2) v_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 %             v_figbolden(figsize);
    0012 %             if emf, v_fig2pdf(figdir), end
    0013 %
    0014 % Inputs: h   optional figure handle [Use [] or omit or gcf for the current figure]
    0015 %         s   file name which can include <m> for the top level mfile name and <n>
    0016 %             for the figure number. If s ends in '/' or '\', then '<m>_<n>' is appended.
    0017 %             Use s='.' to suppress the save. [Use '[]' for s='<m>_<n>']
    0018 %         p   call v_figbolden(p) before printing the figure (use p=0 for v_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: v_fig2pdf.m 10865 2018-09-21 17:22:45Z 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 Lesser General Public License as published by
    0037 %   the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
    0044 %
    0045 %   You can obtain a copy of the GNU Lesser General Public License from
    0046 %   https://www.gnu.org/licenses/ .
    0047 %
    0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0049 switch nargin
    0050     case 0
    0051         h=[];
    0052         s='';
    0053         p=[];
    0054         f='p';
    0055     case 1
    0056         if ischar(h) || ~numel(h)   % v_fig2pdf(s)
    0057             s=h;
    0058             h=[];
    0059         else                        % v_fig2pdf(h)
    0060             s='';
    0061         end
    0062         p=[];
    0063         f='p';
    0064     case 2
    0065         if ischar(h) || ~numel(h)   % v_fig2pdf(s,p)
    0066             p=s;
    0067             s=h;
    0068             h=[];
    0069         else                        % v_fig2pdf(h,s)
    0070             p=[];
    0071         end
    0072         f='p';
    0073     case 3
    0074         if ischar(h) || ~numel(h)   % v_fig2pdf(s,p,f)
    0075             f=p;
    0076             p=s;
    0077             s=h;
    0078             h=[];
    0079         else                        % v_fig2pdf(h,s,p)
    0080             f='p';
    0081         end
    0082 end
    0083 if ~numel(h)
    0084     h=gcf;
    0085 else
    0086     figure(h);
    0087 end
    0088 if ~numel(s)
    0089     s='<m>_<n>';
    0090 elseif s(end)=='/' || s(end)=='\'
    0091     s=[s '<m>_<n>'];
    0092 end
    0093 [st,sti]=dbstack;
    0094 if numel(st)>1
    0095     mfn=st(end).name;  % ancestor mfile name
    0096 else
    0097     mfn='Figure';
    0098 end
    0099 if isreal(h)
    0100     fn=num2str(round(h)); % get figure number
    0101 else
    0102     fn=num2str(get(h,'number'));  % in new versions of matlab use this method
    0103 end
    0104 ix=strfind(s,'<m>');
    0105 while numel(ix)>0
    0106     s=[s(1:ix-1) mfn s(ix+3:end)];
    0107     ix=strfind(s,'<m>');
    0108 end
    0109 ix=strfind(s,'<n>');
    0110 while numel(ix)>0
    0111     s=[s(1:ix-1) fn s(ix+3:end)];
    0112     ix=strfind(s,'<n>');
    0113 end
    0114 if numel(p)>0
    0115     if numel(p)==1 && p==0
    0116         v_figbolden;
    0117     else
    0118         v_figbolden(p)
    0119     end
    0120 end
    0121 set(gcf,'paperpositionmode','auto');    % preserve screen shape
    0122 if ~strcmp(s,'.')
    0123     if isempty(f)
    0124         f='p'; % default is pdf
    0125     end
    0126     sp=[s  '.pdf'];
    0127     print('-dpdf',sp);
    0128     %     set(gcf,'PaperPosition',[0.6350 6.3500 20.3200 12]);
    0129     [cst,cou]=system(['pdfcrop ' sp ' ' sp]); % needs MikTeX installed
    0130     if cst
    0131         error('pdfcrop command failed');
    0132     end
    0133     if any(f=='s')
    0134         [cst,cou]=system(['pdf2ps ' sp ' ' s '.ps']);
    0135     end
    0136     if any(f=='e')
    0137         [cst,cou]=system(['pdf2ps ' sp ' ' s '.eps']);
    0138     end
    0139     if ~any(f=='p')
    0140         [cst,cou]=system(['del ' sp]);
    0141     end
    0142 end