Mercurial > repos > public > sbplib
annotate saveeps.m @ 1223:9fddc8749445 rv_diffOp_test
Closing branch
| author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
|---|---|
| date | Mon, 05 Aug 2019 10:48:37 +0200 |
| parents | a2b95af82f68 |
| children |
| rev | line source |
|---|---|
|
43
a2b95af82f68
Corrected description of saveeps.m
Jonatan Werpers <jonatan@werpers.com>
parents:
34
diff
changeset
|
1 % Saves a figure to an .eps file with corrected bounding box. |
|
a2b95af82f68
Corrected description of saveeps.m
Jonatan Werpers <jonatan@werpers.com>
parents:
34
diff
changeset
|
2 % e.g. saveeps(gcf,'myfig.eps') |
| 0 | 3 function saveeps(handle,filename) |
| 4 if length(filename) < 4 || ~strcmp(filename(end-3:end),'.eps') | |
| 5 filename = [filename '.eps']; | |
| 6 end | |
| 7 | |
| 8 handle_units = handle.Units; % Save the current units to be able to restore | |
| 9 | |
| 10 % Copy size of figure in centimeters to a place where saveas will honor it | |
| 11 handle.Units = 'centimeters'; | |
| 12 handle.PaperUnits = 'centimeters'; | |
| 13 handle.PaperPosition(3:4) = handle.Position(3:4); | |
| 14 | |
| 15 % Save as a bugged eps file. | |
| 16 saveas(handle,filename,'epsc'); | |
| 17 | |
| 18 handle.Units = handle_units; % Restore the old units | |
| 19 | |
| 20 % Correct the buggy eps file | |
| 21 correct_stupid_matlab_bug(filename); | |
| 22 end | |
| 23 | |
| 24 % Corrects the format of an eps file so that the bounding box is defined at the top of the file instead of | |
| 25 % at the bottom. | |
| 26 function correct_stupid_matlab_bug(filename) | |
| 27 contents = fileread(filename); | |
| 28 lines = strsplit(contents,'\n'); | |
| 29 | |
| 30 % Find the line | |
| 31 pagel = findPrefix(lines,'%%Pages:'); | |
| 32 boundl = findPrefix(lines,'%%BoundingBox:'); | |
| 33 | |
|
34
2b4f1d3e5630
Imporved error handling in saveeps. Fixed bug with vector fields in struct2string.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
34 |
| 0 | 35 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
|
36 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
|
37 return |
| 0 | 38 end |
| 39 | |
| 40 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
|
41 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
|
42 return |
| 0 | 43 end |
| 44 | |
| 45 % Overwrite the nasty lines with the nice ones. | |
| 46 lines{pagel(1)} = lines{pagel(2)}; | |
| 47 lines{boundl(1)} = lines{boundl(2)}; | |
| 48 | |
| 49 % Delete the duplicates | |
| 50 lines(pagel(2)) = []; | |
| 51 lines(boundl(2)) = []; | |
| 52 | |
| 53 | |
| 54 %Rewrite the file | |
| 55 contents = strjoin(lines,'\n'); | |
| 56 | |
| 57 fh = fopen(filename,'w'); | |
| 58 fprintf(fh, '%s',contents); | |
| 59 fclose(fh); | |
| 60 end | |
| 61 | |
| 62 function I = findPrefix(lines, prefix) | |
| 63 I = find(strncmp(lines,prefix,length(prefix))); | |
| 64 end |
