comparison saveeps.m @ 0:48b6fb693025

Initial commit.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 17 Sep 2015 10:12:50 +0200
parents
children 2b4f1d3e5630
comparison
equal deleted inserted replaced
-1:000000000000 0:48b6fb693025
1 % Saves a figure to an .eos file with corrected bounding box.
2 function saveeps(handle,filename)
3 if length(filename) < 4 || ~strcmp(filename(end-3:end),'.eps')
4 filename = [filename '.eps'];
5 end
6
7 handle_units = handle.Units; % Save the current units to be able to restore
8
9 % Copy size of figure in centimeters to a place where saveas will honor it
10 handle.Units = 'centimeters';
11 handle.PaperUnits = 'centimeters';
12 handle.PaperPosition(3:4) = handle.Position(3:4);
13
14 % Save as a bugged eps file.
15 saveas(handle,filename,'epsc');
16
17 handle.Units = handle_units; % Restore the old units
18
19 % Correct the buggy eps file
20 correct_stupid_matlab_bug(filename);
21 end
22
23 % Corrects the format of an eps file so that the bounding box is defined at the top of the file instead of
24 % at the bottom.
25 function correct_stupid_matlab_bug(filename)
26 contents = fileread(filename);
27 lines = strsplit(contents,'\n');
28
29 % Find the line
30 pagel = findPrefix(lines,'%%Pages:');
31 boundl = findPrefix(lines,'%%BoundingBox:');
32
33 if ~(length(pagel) == 2 && length(boundl) == 2)
34 error('Undexpected number of found lines');
35 end
36
37 if ~(strcmp(lines{pagel(1)},'%%Pages: (atend)') && strcmp(lines{boundl(1)},'%%BoundingBox: (atend)'))
38 error('Does the file really contain the error?');
39 end
40
41 % Overwrite the nasty lines with the nice ones.
42 lines{pagel(1)} = lines{pagel(2)};
43 lines{boundl(1)} = lines{boundl(2)};
44
45 % Delete the duplicates
46 lines(pagel(2)) = [];
47 lines(boundl(2)) = [];
48
49
50 %Rewrite the file
51 contents = strjoin(lines,'\n');
52
53 fh = fopen(filename,'w');
54 fprintf(fh, '%s',contents);
55 fclose(fh);
56 end
57
58 function I = findPrefix(lines, prefix)
59 I = find(strncmp(lines,prefix,length(prefix)));
60 end