Robochameleon  v1.0
fixPSlinestyle.m
1 function fixPSlinestyle(varargin)
2 
3 %FIXPSLINESTYLE Fix line styles in exported post script files
4 %
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).
10 %
11 % FIXPSLINESTYLE(FILENAME, NEWFILENAME) creates a new file NEWFILENAME.
12 %
13 % This is meant to be used with postscript files created by MATLAB
14 % (print, export).
15 %
16 % Example:
17 % x = 1:.1:10;
18 % y1 = sin(x);
19 % y2 = cos(x);
20 % h = plot(x, y1, '--', x, y2, '-.');
21 % set(h, 'LineWidth', 2);
22 % grid on;
23 % legend('line 1', 'line2');
24 %
25 % print -depsc test.eps
26 % fixPSlinestyle('test.eps', 'fixed_test.eps');
27 %
28 % See also PRINT.
29 
30 % Copyright 2005-2010 The MathWorks, Inc.
31 
32 % Error checking
33 narginchk(1, 2);
34 if ~ischar(varargin{1}) || (nargin == 2 && ~ischar(varargin{2}))
35  error('Input arguments must be file names (char).');
36 end
37 
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');
42 end
43 
44 % Open file and read it in
45 fid = fopen(varargin{1}, 'r');
46 str = fread(fid);
47 str = char(str');
48 fclose(fid);
49 
50 % Find where the line types are defined
51 id = findstr(str, '% line types:');
52 str1 = str(1:id-1);
53 [line1 , remline ] = strtok(str(id:end), '/');
54 [replacestr, remline2] = strtok(remline , '%');
55 
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');
61 
62 % Construct the new file with the new line style definitions
63 newText = [str1, line1, solidLine, dotLine, dashedLine, dashdotLine, remline2];
64 
65 % Check for output file name
66 if nargin == 2
67  [p2, n2, e2] = fileparts(varargin{2});
68  if isempty(e2)
69  fname = fullfile(p2, [n2, e1]);
70  else
71  if strcmpi(e1, e2)
72  fname = varargin{2};
73  else
74  error('Output file must have same file extension.');
75  end
76  end
77 else % if not defined, over-write
78  fname = varargin{1};
79 end
80 
81 % Write out to file
82 fid = fopen(fname, 'w');
83 fprintf(fid, '%s', newText);
84 fclose(fid);