%PLOT2PDF Create a pdf of a figure, using laprint
% PLOT2PDF(figure_handle, filename) Creates a pdf file with the
% specified filename, containing the figure. Latex strings will be
% completely parsed using the setting from 'WRAPPER.TEX'. Note that in the
% standard file, also a .STY file is included. Both files should be
% available in the directory.
%
% PLOT2PDF(figure_handle, filename, option_a, value_a, option_b, value_b,...)
% Uses option and value pairs to toggle some additional settings.
%
% figure_handle [integer] : Handle of the figure to be converted to pdf
% filename [Character array] : Basename of the file to be created (.pdf)
% is added automatically
% Option list:
% 'verbose' - 'On' or 'Off'
% If 'On'it shows a lot more information, which can be
% usefull for finding syntax errors in LaTeX strings.
% 'legend' - Cell array of character strings, p.e. {'line1','line2'}
% Use LaTeX to create a legend in Math mode. Can be
% usefull if the legend should contain some formulas.
% 'legend_pos' - [Xpos Ypos]
% Position of the legend relative to the figure axes.
% where [0 0] represents the bottom left, and [1 1]
% represents the top right of the figure.
% 'legend_colors' - [R1 G1 B1; R2 G2 B2]
% Specify the color of the lines, in RGB value. so the
% first line gets color [R1 G1 B1], the second line
% [R2 G2 B2], and so on.
function out = plot2pdf(figure_handle, filename, varargin)
%%%% CHECK INPUTS %%%%
if nargin < 2
error('Specify at least 2 inputs: the figure handle and the filename.');
end
% Figure handle
if ~isa(figure_handle,'double')
figure_handle
error('This is not a figure handle.')
end
if ~any(get(0,'children')==figure_handle)
figure_handle
error('This is not a figure handle.')
end
% Filename
if strfind(filename, '.') > 0
error('No periods allowed in filename: Do''nt specify an extension.')
end
if ~isa(filename,'char')
filename
error('This is not a file name.')
end
fig_name = filename;
% set defaults
legend_switch = 0;
legend_pos = 0;
legend_abs_pos = 0;
legend_colors = 0;
verbose = 0;
object_opts = '''VerticalAlignment'', ''top'', ''HorizontalAlignment'', ''right''';
legend_percentual_x = 0.98;
legend_percentual_y = 0.98;
% Various arguments
if nargin > 2
if rem(nargin,2)
error('Option names/values must appear in pairs.')
end
for i=1:2:nargin-2
if ~isa(varargin{i},'char')
error('Option name must be a character array.')
end
opt = lower(strrep(varargin{i}(:)',' ',''));
val = varargin{i+1}(:)';
switch opt
% case 'options'
% if isa(val,'char')
% if strcmp(val,'factory')
% val = factorysettings;
% else
% load(val)
% val = LAPRINTOPT;
% end
% end
% if ~isa(val,'struct')
% error('Value of options must be a structure array.')
% end
% % no error checking here!
% width = val.width;
% factor = val.factor;
% scalefonts = val.scalefonts;
% keepfontprops = val.keepfontprops;
% asonscreen = val.asonscreen;
% keepticklabels = val.keepticklabels;
% mathticklabels = val.mathticklabels;
% head = val.head;
% comment = val.comment;
% caption = val.caption;
% extrapicture = val.extrapicture;
% nzeros = val.nzeros;
% verbose = val.verbose;
% figcopy = val.figcopy;
% printcmd = val.printcmd;
% package = val.package;
% color = val.color;
% createview = val.createview;
% viewfilename = val.viewfilename;
% processview = val.processview;
% cmd1 = val.cmd1;
% cmd2 = val.cmd2;
% cmd3 = val.cmd3;
% cmd4 = val.cmd4;
% cmd5 = val.cmd5;
% cmd6 = val.cmd6;
% cmd7 = val.cmd7;
% cmd8 = val.cmd8;
case 'verbose'
verbose = value01(val,opt);
case 'legend'
if ~isa(val,'cell')
error('Value of printcmd must be a cell array.')
end
legendstring = val;
legend_switch = 1;
case 'legend_pos'
if ~legend_switch
error('No use of specifying a legend_pos without a legend.')
end
if isa(val,'numeric')
if ~all(size(val) == [1 2])
error('Values of legend_pos are not correctly specified')
elseif max(val) > 1 || min(val) < 0
error('Values of legend_pos are not of correct size (between 0 and 1)')
end
legend_pos = 1;
legend_percentual_x = val(1);
legend_percentual_y = val(2);
elseif isa(val,'char')
legend_pos = 1;
corner_dist = 0.05;
switch lower(val)
case 'north'
legend_percentual_x = 0.5;
legend_percentual_y = 1-corner_dist;
object_opts = '''VerticalAlignment'', ''top'', ''HorizontalAlignment'', ''center''';
case 'south'
legend_percentual_x = 0.5;
legend_percentual_y = corner_dist;
object_opts = '''VerticalAlignment'', ''bottom'', ''HorizontalAlignment'', ''center''';
case 'southeast'
legend_percentual_x = 1-corner_dist;
legend_percentual_y = corner_dist;
object_opts = '''VerticalAlignment'', ''bottom'', ''HorizontalAlignment'', ''right''';
case 'southwest'
legend_percentual_x = corner_dist;
legend_percentual_y = corner_dist;
object_opts = '''VerticalAlignment'', ''bottom'', ''HorizontalAlignment'', ''left''';
case 'northeast'
legend_percentual_x = 1-corner_dist;
legend_percentual_y = 1-corner_dist;
object_opts = '''VerticalAlignment'', ''top'', ''HorizontalAlignment'', ''right''';
case 'northwest'
legend_percentual_x = corner_dist;
legend_percentual_y = 1-corner_dist;
object_opts = '''VerticalAlignment'', ''top'', ''HorizontalAlignment'', ''left''';
case 'east'
legend_percentual_x = 1-corner_dist;
legend_percentual_y = 0.5;
object_opts = '''VerticalAlignment'', ''middle'', ''HorizontalAlignment'', ''right''';
case 'west'
legend_percentual_x = corner_dist;
legend_percentual_y = 0.5;
object_opts = '''VerticalAlignment'', ''middle'', ''HorizontalAlignment'', ''left''';
otherwise
error('legend value unknown')
end
else
error('Not a valid value for ''legend''')
end
case 'legend_colors'
if ~legend_switch
error('No use of specifying legend_colors without a legend.')
elseif length(val) ~= length(legendstring)
error('Number of colors does not coincide with the number of legend entries')
elseif ~isa(val,'cell')
error('Specify the colors in a cell')
end
legend_colors = 1;
linecolors = zeros(length(val),3);
for i = 1:length(val)
if size(val{i},2) ~= 3 || ~isa(val{i},'numeric')
error('Not a valid RGB value')
end
linecolors(i,:) = val{i};
end
case 'abs_legend_pos'
if ~all(size(val) == [1 2])
error('Values of legend_pos are not correctly specified')
end
legend_abs_pos = 1;
legend_abs_x = val(1);
legend_abs_y = val(2);
otherwise
error(['Option ''' opt ''' unknown'])
end % switch opt
end % for i=3:2:nargin
end % if nargin > 2
if legend_switch
% Check line colors
if ~legend_colors
linecolors = colormap('lines');
end
% Construct LaTeX code for the legend
finallegendstring = [];
for i = 1:length(legendstring)
finallegendstring = [finallegendstring sprintf('\\textcolor[rgb]{%s}{\\rule[0.5ex]{1.8em}{2pt}} & %s', regexprep(num2str(linecolors(i,:)),'\s*\s',','), legendstring{i})];
if i ~= length(legendstring)
finallegendstring = [finallegendstring '\\'];
end
end
legendx = min(get(gca,'XLim'))+legend_percentual_x*(max(get(gca,'XLim')) - min(get(gca,'XLim')));
legendy = min(get(gca,'YLim'))+legend_percentual_y*(max(get(gca,'YLim')) - min(get(gca,'YLim')));
if legend_abs_pos
legendx = legend_abs_x;
legendy = legend_abs_y;
end
% print the legend
figure(figure_handle)
labels = findobj(figure_handle,'Type','text');
delete(labels);
legend_lbl = text(legendx, legendy, sprintf('\\setlength{\\mathsurround}{5pt}\\setlength{\\fboxrule}{0.5pt}\\fcolorbox{black}{white}{$\\begin{array}{cl} %s \\end{array}$}', finallegendstring));
eval(sprintf('set(legend_lbl, %s);', object_opts));
end % legend_switch
laprint(figure_handle,'C:\Documents and Settings\s031080\My Documents\MATLAB\plot2pdf\figure1', 'mathticklabels', 'off')
old_dir = pwd;
if verbose
system('cd C:\Documents and Settings\s031080\My Documents\MATLAB\plot2pdf')
cd('C:\Documents and Settings\s031080\My Documents\MATLAB\plot2pdf')
system('latex "wrapper.tex"')
system('dvips -I c "wrapper.dvi"')
system('"C:\Program Files\gs\gs8.61\bin\gswin32c.exe" -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -r600 -dCompatibilityLevel=1.4 -sOutputFile="wrapper.pdf" -c save pop -f "wrapper.ps"')
system(sprintf('perl pdfcrop.pl wrapper.pdf %s.pdf',fig_name))
system(sprintf('copy %s.pdf "%s\\"',fig_name,old_dir))
system(sprintf('del %s.pdf',fig_name))
system('del wrapper.dvi');
system('del wrapper.out');
system('del wrapper.aux');
system('del wrapper.log');
system('del wrapper.pfg');
system('del wrapper.ps');
system('del wrapper.pdf');
system('del figure1.tex');
system('del figure1.eps');
system('del tmp-pdfcrop-*');
system(sprintf('cd %s\\',old_dir))
cd(old_dir)
else
[status,result] = system('cd C:\Documents and Settings\s031080\My Documents\MATLAB\plot2pdf');
cd('C:\Documents and Settings\s031080\My Documents\MATLAB\plot2pdf');
[status,result] = system('latex "wrapper.tex"');
[status,result] = system('dvips -I c "wrapper.dvi"');
[status,result] = system('"C:\Program Files\gs\gs8.61\bin\gswin32c.exe" -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -r600 -dCompatibilityLevel=1.4 -sOutputFile="wrapper.pdf" -c save pop -f "wrapper.ps"');
[status,result] = system(sprintf('perl pdfcrop.pl wrapper.pdf %s.pdf',fig_name));
[status,result] = system(sprintf('copy %s.pdf "%s\\"',fig_name,old_dir));
[status,result] = system(sprintf('del %s.pdf',fig_name));
[status,result] = system('del wrapper.dvi');
[status,result] = system('del wrapper.out');
[status,result] = system('del wrapper.aux');
[status,result] = system('del wrapper.log');
[status,result] = system('del wrapper.pfg');
[status,result] = system('del wrapper.ps');
[status,result] = system('del wrapper.pdf');
[status,result] = system('del wrapper.pdf');
[status,result] = system('del figure1.tex');
[status,result] = system('del figure1.eps');
[status,result] = system('del tmp-pdfcrop-*');
[status,result] = system(sprintf('cd %s\\',old_dir));
cd(old_dir);
end % verbose
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = value01(val,opt)
% convert off/on to 0/1
if nargin==2
txt = ['Value of ' opt ' must be ''on'' or ''off'''];
else
txt = ['Value must be ''on'' or ''off'''];
end
if ~isa(val,'char')
error(txt)
end
val = lower(strrep(val,' ',''));
switch lower(val)
case 'on'
v = 1;
case 'off'
v = 0;
otherwise
error(txt)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%