%SAVER   save a figure 'wysiwyg' in pdf (and optionally in eps)   
%   
%   Usage:   saver(name,fignumber,path,save_eps)
%
%   Inputs:  name:      name of the file (without extension)
%            fignumber: figure number (default: current figure)
%            path:      where to save the file (default: current dir)
%            save_eps:  [ 1 | 0 (default) ] also save as .eps file 
%   
%   Outputs: name.pdf (optionally also name.eps)
%   
%   See also SETPLOT.
   
%   Changes: 
%   2011083 - Initial version (Rob Hoogendijk)
%   
%   Rob Hoogendijk (2011-08-3)%   
%   ________________________________
%   Eindhoven University of Technology
%   Dept. of Mechanical Engineering
%   Control Systems Technology group
%   PO Box 513, WH -1.126
%   5600 MB Eindhoven, NL
%   T +31 (0)40 247 4227
%   F +31 (0)40 246 1418
%   E r.hoogendijk@tue.nl

function saver(name,handle,path,eps) 
%use current figure if no handle is supplied 
if (nargin<2) 
    handle=gcf; 
end 
%default path current dir 
if (nargin<3) 
    if(isunix)         
        path=[cd '/']; %for linux users 
    else 
        path=[cd '\']; %for windows users 
    end 
end 
%store eps as well 
if(nargin<4) 
    eps=0; 
end 
 
%wysiwyg part... 
%store old units 
oldpaperunits=get(handle,'PaperUnits'); 
oldunits=get(handle,'Units'); 
%set papersize 
set(handle,'PaperUnits','centimeters'); 
set(handle,'Units','centimeters'); 
currentpos = get(handle,'Position'); 
set(handle,'Paperposition',[0 0 currentpos(3) currentpos(4)]); 
set(handle,'PaperSize',[currentpos(3) currentpos(4)]); 
%restore old units 
set(handle,'PaperUnits',oldpaperunits); 
set(handle,'Units',oldunits); 
 
% save eps 
if(eps) 
    print(handle,'-depsc2',[path,name,'.eps']); 
    disp(['Figure ' num2str(handle) ' stored in ' path,name '.eps']); 
end 
 
% save pdf 
if exist([path,name,'.pdf'])
    R=input(['Overwrite file ' path, name '.pdf? [y/n] : '],'s'); 
    if(R=='y' || isempty(R)) 
        try 
            chan=ddeinit('acroview','control'); 
            if chan 
                ddeexec(chan,[ '[DocOpen("' path , name '.pdf")]' ]); 
                ddeexec(chan,[ '[DocClose("' path , name '.pdf")]' ]); 
            end; 
            delete([path, name '.pdf']) 
        catch 
            delete([path, name '.pdf']); 
        end 
    else 
        error('Figure not saved.'); 
    end 
end
warning off; 
print(handle,'-dpdf',[path,name,'.pdf']); 
warning on; 
disp(['Figure ' num2str(handle) ' stored in ' path,name '.pdf']); 
