1 function fixPSlinestyle(varargin)
3 %FIXPSLINESTYLE Fix line styles in exported post script files
5 % FIXPSLINESTYLE(FILENAME) fixes the line styles in the postscript file
6 % FILENAME. The file will be over-written. This takes a .PS or .EPS file
7 % and fixes the dotted and dashed line styles to be a little bit more
8 % esthetically pleasing. It fixes the four default line styles (line,
9 % dotted, dashed, dashdot).
11 % FIXPSLINESTYLE(FILENAME, NEWFILENAME) creates a new file NEWFILENAME.
13 % This is meant to be used with postscript files created by MATLAB
20 % h = plot(x, y1, '--', x, y2, '-.');
21 % set(h, 'LineWidth', 2);
23 % legend('line 1', 'line2');
25 % print -depsc test.eps
26 % fixPSlinestyle('test.eps', 'fixed_test.eps');
30 % Copyright 2005-2010 The MathWorks, Inc.
34 if ~ischar(varargin{1}) || (nargin == 2 && ~ischar(varargin{2}))
35 error('Input arguments must be file names (
char).');
38 % Make sure the files specified are postscript files
39 [p1, n1, e1] = fileparts(varargin{1});
40 if isempty(e1) || ~ismember(lower(e1), {
'.ps',
'.eps'})
41 error(
'The extension has to be .ps or .eps');
44 % Open file and read it in
45 fid = fopen(varargin{1},
'r');
50 % Find where the line types are defined 51 id = findstr(str, '% line types:
'); 53 [line1 , remline ] = strtok(str(id:end), '/
'); 54 [replacestr, remline2] = strtok(remline , '%
'); 56 % Define the new line styles 57 solidLine = sprintf('/SO { [] 0 setdash } bdef\n
'); 58 dotLine = sprintf('/DO { [3 dpi2point mul 3 dpi2point mul] 0 setdash } bdef\n
'); 59 dashedLine = sprintf('/DA { [6 dpi2point mul] 0 setdash } bdef\n
'); 60 dashdotLine = sprintf('/DD { [2 dpi2point mul 2 dpi2point mul 6 dpi2point mul 2 dpi2point mul] 0 setdash } bdef\n
'); 62 % Construct the new file with the new line style definitions 63 newText = [str1, line1, solidLine, dotLine, dashedLine, dashdotLine, remline2]; 65 % Check for output file name 67 [p2, n2, e2] = fileparts(varargin{2}); 69 fname = fullfile(p2, [n2, e1]); 74 error('Output file must have same file extension.
'); 77 else % if not defined, over-write 82 fid = fopen(fname, 'w
'); 83 fprintf(fid, '%s
', newText);