Code covered by the BSD License  

Highlights from
Huygens-Fresnel integral approximation, free-form apertures and rough surfaces

image thumbnail

Huygens-Fresnel integral approximation, free-form apertures and rough surfaces

by

 

Simulate wave propagation through free-form apertures, or off rough surfaces.

mlf2pdf(varargin)
% MLF2PDF prints a PDF figure for inclusion in LaTeX documents, with
%    LaTeX fonts. It creates a complete pdf-file with annotation of the 
%    figure (titles, labels and texts) and graphics (lines,arrows,markers,
%    ...). This function uses the matlabfrag function and makes simply a
%    concatenation of the text part and the graphical part in a single PDF
%    file. It requires:
%
%                  MATLAB        |      OTHER
%               -----------------|----------------------------
%                 matlabfrag.m   |      pdflatex
%
%    
%    USAGE:
%    ------------------------
%
%    mlf2pdf creates the PDF file of the current graphical figure (CGF) 
%    named LaTeXfile.pdf
% 
%    mlf2pdf(H) creates the PDF file from the graphical figure with 
%    handle H, named LaTeXfile.pdf
%    
%    mlf2pdf(H,FILENAME) creates the PDF file from the graphical figure 
%    with handle H, named FILENAME.PDF (FILENAME is a character array of
%    the filename, with or without the extension .PDF)
%   
%    mlf2pdf(H,FILENAME,PACKAGES) to adding
%    packages such as fonts, mathfont,...
%    ex: PACKAGES = 'amssymb, times'
% 

function mlf2pdf(varargin)

p = inputParser;
addOptional(p, 'handle', gcf, @ishandle);
addOptional(p, 'filename', 'LaTeXfile', @isstr);
addParamValue(p, 'packages', {}, @(x) iscell(x));
addParamValue(p, 'fontsize', 11, @isnumeric);
addParamValue(p, 'crop', true, @islogical)
addParamValue(p, 'useOpengl', false, @islogical)

parse(p, varargin{:});
h = p.Results.handle;
FileName = p.Results.filename;
Packages = p.Results.packages;
fontsize = p.Results.fontsize;
crop = p.Results.crop;
useOpengl = p.Results.useOpengl;

if ~isempty(Packages) && ~iscell(Packages{1})
	Packages = {Packages};
end

TempName = strcat('TEMP',num2str(round(rand*10000))); %Generate random file name

if ~exist('matlabfrag.m')
    disp('MatLabFrag M-file does not exist or is not in the MATLAB''s search path.');
    disp('This file can be downloaded at: http://www.mathworks.com/matlabcentral/fileexchange');
    disp('                                                         Try again...');
    return;
end

if useOpengl
    matlabfrag(TempName,'handle',h,'renderer','opengl','dpi',720);  %call matlabfrag to export figure to .eps and .tex file.
else
    matlabfrag(TempName,'handle',h);
end
 
%-------------------------------------------------------
% Temporary LaTeX file
%-------------------------------------------------------
fid = fopen(strcat(TempName,'2.tex'),'w');

fprintf(fid,'\\documentclass[%spt, oneside]{article}\n',num2str(fontsize));
fprintf(fid,'\\usepackage[T1]{fontenc}\n');
fprintf(fid,'\\usepackage[latin1]{inputenc}\n');
fprintf(fid,'\\usepackage{lmodern}\n');
fprintf(fid,'\\usepackage{graphicx, xcolor, psfrag}\n');
fprintf(fid,'\\usepackage{amsmath}\n');

for k = 1:length(Packages)					% Suplementary packages
	if isempty(Packages{k}{1})
		fprintf(fid,'\\usepackage{%s}\n', Packages{k}{2});
	else		
		fprintf(fid,'\\usepackage[%s]{%s}\n',Packages{k}{1}, Packages{k}{2});
	end
end

fprintf(fid,'\\pagestyle{empty}\n');
fprintf(fid,'\\begin{document}\n');
fprintf(fid,'    \\begin{figure}\n');
fprintf(fid,'        \\centering\n');
fprintf(fid,'        \\input{%s}\n',TempName);
fprintf(fid,'        \\includegraphics{%s}\n',TempName);
fprintf(fid,'    \\end{figure}\n');
fprintf(fid,'\\end{document}\n');
fclose(fid);

%-------------------------------------------------------
% LaTeX Command
%-------------------------------------------------------
Str=sprintf('latex -shell-escape --src -interaction=nonstopmode %s2.tex',TempName);
disp(sprintf('\n[LaTeX Command] %s',Str));
[hdos,wdos]=system(Str);

if hdos ~=0
   if isunix==0
     dos(sprintf('del %s*',TempName));  
   else
     unix(sprintf('rm %s*',TempName));
   end
   error('Error %d -- LATEX:\n%s',hdos ,wdos);
   return;
end

%-------------------------------------------------------
% Convert to eps
%-------------------------------------------------------
Str=sprintf('dvips -Ppdf -E -o %s2.eps %s2.dvi', TempName, TempName);
disp(sprintf('\n[dvips Command] %s',Str));
[hdos,wdos]=system(Str);

if hdos ~=0
   if isunix==0
     dos(sprintf('del %s*',TempName));  
   else
     unix(sprintf('rm %s*',TempName));
   end
   error('Error %d -- dvips:\n%s',hdos ,wdos);
   return;
end
 
%-------------------------------------------------------
% clip image to its bounding box
%-------------------------------------------------------
Str=sprintf('epstool --copy --bbox %s2.eps %s3.eps', TempName, TempName);
disp(sprintf('\n[LaTeX Command] %s',Str));
[hdos,wdos]=system(Str);

if hdos ~=0
   if isunix==0
     dos(sprintf('del %s*',TempName));  
   else
     unix(sprintf('rm %s*',TempName));
   end
   error('Error %d -- epstool:\n%s',hdos ,wdos);
   return;
end

%-------------------------------------------------------
% convert image to pdf
%-------------------------------------------------------
if crop
    Str=sprintf('gswin64c -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=pdfwrite -sOutputFile=%s.pdf -c save pop -f %s3.eps -c quit', TempName, TempName);
else
    Str=sprintf('gswin64c -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=pdfwrite -sOutputFile=%s.pdf -c save pop -f %s2.eps -c quit', TempName, TempName);
end
disp(sprintf('\n[gswin64c Command] %s',Str));
[hdos,wdos]=system(Str);

hdos2 = 0;
wdos2 = 0;
if hdos ~= 0
	Str=sprintf('gswin32c -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=pdfwrite -sOutputFile=%s.pdf -c save pop -f %s3.eps -c quit', TempName, TempName);
	disp(sprintf('\n[gswin32c Command] %s',Str));
	[hdos2,wdos2]=system(Str);
end

if (hdos ~= 0 && hdos2 ~= 0)
   if isunix==0
     dos(sprintf('del %s*',TempName));  
   else
     unix(sprintf('rm %s*',TempName));
   end
   error('Error %d -- gswin32/64c:\n%s\n\nError %d -- gswin32/64c:\n%s',hdos ,wdos,hdos2 ,wdos2);
   return;
end

%-------------------------------------------------------
% Rename TempFile to FileName and delete FileName before (if it exists)
%-------------------------------------------------------
if isunix==0
   dos(sprintf('del %s.pdf',FileName));
   dos(sprintf('ren %s.pdf %s.pdf',TempName,FileName)); 
else 
   unix(sprintf('rm %s.pdf',FileName));
   unix(sprintf('mv %s.pdf %s.pdf',TempName,FileName));
end

%-------------------------------------------------------
% Success
%------------------------------------------------------- 
  disp(sprintf('... OK!\nPDF file [%s.pdf] has been created in the current directory\n',FileName));
 %-------------------------------------------------------
% Delete all the temporary files
%-------------------------------------------------------
if isunix==0
  dos(sprintf('del %s*',TempName));
else
  unix(sprintf('rm %s*',TempName));
end
return;

Contact us