Debian Codegraph @

test/private/execute_save_stage.m

raw
1 function [status] = execute_save_stage(status, ipp)
2 % save stage: saves the figure to EPS/PDF depending on env
3 testNumber = status.index;
4
5 basepath = fullfile(ipp.Results.output,'data','reference');
6 reference_eps = fullfile(basepath, sprintf('test%d-reference.eps', testNumber));
7 reference_pdf = fullfile(basepath, sprintf('test%d-reference.pdf', testNumber));
8 % the reference below is for inclusion in LaTeX! Use UNIX conventions!
9 reference_fig = sprintf('data/reference/test%d-reference', testNumber);
10
11 % Save reference output as PDF
12 try
13 switch getEnvironment
14 case 'MATLAB'
15 % MATLAB does not generate properly cropped PDF files.
16 % So, we generate EPS files that are converted later on.
17 print(gcf, '-depsc2', reference_eps);
18
19 fixLineEndingsInWindows(reference_eps);
20
21 case 'Octave'
22 % In Octave, figures are properly cropped when using print().
23 print(reference_pdf, '-dpdf', '-S415,311', '-r150');
24 pause(1.0)
25 otherwise
26 error('matlab2tikz:UnknownEnvironment', ...
27 'Unknown environment. Need MATLAB(R) or GNU Octave.')
28 end
29 catch %#ok
30 e = lasterror('reset'); %#ok
31 [status.saveStage] = errorHandler(e);
32 end
33 status.saveStage.epsFile = reference_eps;
34 status.saveStage.pdfFile = reference_pdf;
35 status.saveStage.texReference = reference_fig;
36 end
37 % ==============================================================================
38 function fixLineEndingsInWindows(filename)
39 % On R2014b Win, line endings in .eps are Unix style (LF) instead of Windows
40 % style (CR+LF). This causes problems in the MikTeX `epstopdf` for some files
41 % as dicussed in:
42 % * https://github.com/matlab2tikz/matlab2tikz/issues/370
43 % * http://tex.stackexchange.com/questions/208179
44 if ispc
45 fid = fopen(filename,'r+');
46 finally_fclose_fid = onCleanup(@() fclose(fid));
47 testline = fgets(fid);
48 CRLF = sprintf('\r\n');
49 endOfLine = testline(end-1:end);
50 if ~strcmpi(endOfLine, CRLF)
51 endOfLine = testline(end); % probably an LF
52
53 % Rewind, read the whole
54 fseek(fid,0,'bof');
55 str = fread(fid,'*char')';
56
57 % Replace, overwrite and close
58 str = strrep(str, endOfLine, CRLF);
59 fseek(fid,0,'bof');
60 fprintf(fid,'%s',str);
61 end
62 end
63 end