Mercurial > repos > public > sbplib
annotate saveeps.m @ 34:2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 06 Oct 2015 09:47:30 +0200 |
parents | 48b6fb693025 |
children | a2b95af82f68 |
rev | line source |
---|---|
0 | 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 | |
34
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
33 |
0 | 34 if ~(length(pagel) == 2 && length(boundl) == 2) |
34
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
35 warning('Undexpected number of found lines: %d , %d\nNot correcting the file',pagel, boundl); |
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
36 return |
0 | 37 end |
38 | |
39 if ~(strcmp(lines{pagel(1)},'%%Pages: (atend)') && strcmp(lines{boundl(1)},'%%BoundingBox: (atend)')) | |
34
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
40 warning('Does the file really contain the error?\nNot correcting the file'); |
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
41 return |
0 | 42 end |
43 | |
44 % Overwrite the nasty lines with the nice ones. | |
45 lines{pagel(1)} = lines{pagel(2)}; | |
46 lines{boundl(1)} = lines{boundl(2)}; | |
47 | |
48 % Delete the duplicates | |
49 lines(pagel(2)) = []; | |
50 lines(boundl(2)) = []; | |
51 | |
52 | |
53 %Rewrite the file | |
54 contents = strjoin(lines,'\n'); | |
55 | |
56 fh = fopen(filename,'w'); | |
57 fprintf(fh, '%s',contents); | |
58 fclose(fh); | |
59 end | |
60 | |
61 function I = findPrefix(lines, prefix) | |
62 I = find(strncmp(lines,prefix,length(prefix))); | |
63 end |