rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
plot_as_tikzfile.m
Go to the documentation of this file.
1 function clim = plot_as_tikzfile(model, params)
2 % function plot_as_tikzfile(model, params)
3 % postprocesses a figure and write out an image and a text file that can be
4 % included in TeX documents.
5 %
6 % This method creates three files from a MATLAB figure specified by
7 % 'params.figure_handle':
8 % - A 'png' coded image file containing a snapshot of the figure box
9 % - A text file ending '.tikz' that can be included in a LaTeX document
10 % adding axes and meta information around the 'png' file.
11 % - A text file ending '_test.tex' that can be compiled with pdflatex and
12 % outputs a pdf file with the figure output.
13 %
14 % Why this is better than including the 'png' file directly:
15 % When scaling images for MATLAB figures, often the text for the ticks,
16 % legends and axes description is scaled to an unreadable size. Furthermore,
17 % text sizes can differ between a LaTeX document and the MATLAB Monospace font
18 % type does not look nice in the PDF output. The solution here, circumvents
19 % all these problems, because all text used inside the figures will be
20 % generated during pdflatex compilation phase.
21 %
22 % parameters:
23 % params: Options controlling the output
24 %
25 % required fields of params:
26 % figure_handle: handle to the figure that shall be postprocessed
27 % filename: base of filename for the three generated files
28 % 'filename.png', 'filename.tikz' and 'filename_test.tex'
29 % filepath: path where the output files shall be stored
30 % width: width of output picture in pixels.
31 %
32 % optional fields of params:
33 % height: height of output picture in pixels.
34 % If this parameter does not exist or is set to zero, the
35 % height is calculated from the width and the pictures ratio.
36 % save_colorbar: boolean specifying wether separate files for the colorbar
37 % is generated.
38 % print_axes: boolean specifying wether the tikz file shall include
39 % drawing commands for axes around the picture.
40 % (Default=true)
41 % print_axes_label: boolean specifying wether the tikz file shall include
42 % drawing commands for labels at the axes. This field is
43 % ignored if 'print_axes' is set to false. (Default=true)
44 % ticks: integer specifying how many ticks should be added to the
45 % axes drawn around the figure. This field is ignored if
46 % 'print_axes' or 'print_axes_label' is set to false.
47 % (Default=5)
48 %
49 fn = params.filename;
50 % dirname
51 if isfield(params, 'filepath')
52  fp = params.filepath;
53 else
54  fp = './';
55 end
56 % figure handle
57 if isfield(params, 'figure_handle')
58  fhandle = params.figure_handle;
59 else
60  fhandle = gcf;
61 end
62 % colorbar
63 if isfield(params, 'save_colorbar') && params.save_colorbar
64  save_colorbar = 1;
65 else
66  save_colorbar = 0;
67 end
68 
69 if ~isfield(params, 'print_axes')
70  params.print_axes = 1;
71 end
72 
73 if ~isfield(params, 'print_axes_label')
74  params.print_axes_label = 1;
75 end
76 
77 if ~isfield(params, 'width')
78  params.width = 8.0;
79 end
80 
81 if ~isfield(params, 'scaleaxis')
82  params.scaleaxis = 'scale only axis';
83 end
84 
85 
86 
87 if isfield(params, 'ticks')
88  ticks = params.ticks;
89 else
90  ticks = 5;
91 end
92 
93 % change figure properties for transparencies
94 set(fhandle,'Color','none');
95 % get axes:
96 children = get(fhandle, 'Children');
97 for i=length(children):-1:1
98  if strcmp(get(children(i), 'Type'), 'axes') == 1
99  set(children(i),'Color','none');
100  ahandle=children(i);
101  end
102 end
103 %colorbar('peer', ahandle);
104 %colorbar(ahandle, 'hide');
105 set(fhandle,'InvertHardCopy','off');
106 showaxes('hide');
107 
108 % export figure to png file
109 im = export_fig(fullfile(fp, fn),'-png','-a3', fhandle);
110 imsize = size(im);
111 
112 % save colorbar as well
113 if save_colorbar
114  colorbarfn = fullfile(fp, [fn, 'colorbar']);
115  if ~exist([colorbarfn, '.png'], 'file')
116  colorh = colorbar('peer',ahandle);
117  yticks = get(colorh, 'YTick');
118  set(colorh, 'YTick',[]);
119  set(colorh, 'YTickLabel',[]);
120 
121  cbim = export_fig(colorbarfn, '-png', '-a3', colorh);
122 % cbim = export_fig(colorh);
123  size(cbim)
124  end
125 end
126 
127 % color ranges
128 col_ranges=cell(2,1);
129 col_ranges{1} = get(gca,'XLim');
130 col_ranges{2} = get(gca,'YLim');
131 clim = get(gca,'CLim');
132 
133 xr = col_ranges{1};
134 yr = col_ranges{2};
135 
136 % x-y axes ratio
137 xyratio = imsize(1)/imsize(2);
138 
139 % tikz file
140 fid = fopen(fullfile(fp, [fn, '.tikz']), 'w+');
141 ftexid = fopen(fullfile(fp, [fn, '_test.tex']), 'w+');
142 
143 width = params.width;
144 if ~isfield(params, 'height') || params.height == 0
145  height = width * xyratio;
146 else
147  height = params.height;
148 end
149 
150 % xtick string
151 xtstr = '';
152 ytstr = '';
153 for i = 1:ticks
154  xtstr = [xtstr, ...
155  sprintf('%.2fcm/%.1f', (width) * (i-1)/(ticks-1),...
156  (xr(2)-xr(1))*(i-1)/(ticks-1) + xr(1))];
157  ytstr = [ytstr, ...
158  sprintf('%.2fcm/%.1f', (height) * (i-1)/(ticks-1),...
159  (yr(2)-yr(1))*(i-1)/(ticks-1) + yr(1))];
160  if i ~= ticks
161  xtstr = [xtstr, ', '];
162  ytstr = [ytstr, ', '];
163  end
164 end
165 
166 if save_colorbar
167  colorbarinput = sprintf(' \\\\begin{scope}[xshift=-2cm]\\\\input{%s}\\n\\\\end{scope}',[colorbarfn, '.tikz']);
168 else
169  colorbarinput = '';
170 end
171 
172 fprintf(ftexid,['\\documentclass{article}\n',...
173  '\\usepackage{tikz,pgfplots}\n',...
174  '\\usetikzlibrary{arrows,snakes,shapes,decorations.pathmorphing,backgrounds,fit}\n',...
175  '\\begin{document}\n',...
176  ' \\begin{tikzpicture}\n',...
177  ' \\input{%s}\n',...
178  colorbarinput,...
179  ' \\end{tikzpicture}\n',...
180  '\\end{document}\n'],[fn, '.tikz']);
181 
182 fclose(ftexid);
183 
184 if params.print_axes && params.print_axes_label
185  ticks = 'data';
186 else
187  ticks = '\empty';
188 end
189 fileinput = sprintf(['\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
190 '\\everymath{\\scriptstyle};\n', ...
191 '\\begin{axis}[axis on top, enlargelimits=false,\n',...
192 'xtick=%s,ytick=%s,',params.scaleaxis,', width=%s cm, height=%s cm]\n',...
193 ' \\addplot graphics[xmin=%s, xmax=%s, ymin=%s, ymax=%s] {%s};\n',...
194 '\\end{axis}\n'],...
195  ticks, ticks, num2str(width), num2str(height), ...
196  num2str(xr(1)), num2str(xr(2)), num2str(yr(1)), num2str(yr(2)), fn);
197 
198 fwrite(fid, fileinput);
199 
200 fclose(fid);
201 
202 if save_colorbar
203  colorfid = fopen(fullfile(fp, [colorbarfn, '.tikz']), 'w+');
204  fileinput = sprintf(['\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
205  '\\everymath{\\scriptstyle};\n', ...
206  '\\begin{axis}[xtick=\\empty,ytick=%s,enlargelimits=false,\n', ...
207  ' height=%s cm,width=50pt]\n', ...
208  ' \\addplot graphics[includegraphics={trim=1 1 1 1},xmin=0, xmax=1,\n', ...
209  ' ymin=%s,ymax=%s]\n', ...
210  ' {%s};\n', ...
211  '\\end{axis}\n'], ...
212  ticks, num2str(height), num2str(clim(1)), num2str(clim(2)), colorbarfn);
213 
214  fwrite(colorfid, fileinput);
215 
216  fclose(fid);
217 end
218 
219 end
function clim = plot_as_tikzfile(model, params)
postprocesses a figure and write out an image and a text file that can be included in TeX documents...