This file is indexed.

/usr/share/texmf-texlive/scripts/pgfplots/matlab2pgfplots.m is in texlive-pictures 2009-15.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function matlab2pgfplots(varargin )
% matlab2pgfplots(outfile )
% matlab2pgfplots( outfile, OPTIONS )
%
% Generate LaTeX code for use in package pgfplots to
% draw line plots.
%
% It will use every (2d) line plot in the figure specified by handler fighandle.
% 
% It understands
% - axis labels,
% - legends,
% - any 2d line plots,
% - line styles/markers (in case of styles=1),
% - tick positions, labels and axis limits (in case of axes=1).
%
% Linestyles and markers will follow as an option. However, pgfplots has its
% own line styles which may be appropriate.
%
% Although pgfplots can also handle bar and area plots, this script is not yet
% capable of converting them. Feel free to modify it and send the final version
% to me!
%
% OPTIONS are key value pairs. Known options are
% - 'fig',HANDLE
%		a figure handle (default is 'gcf').
% - 'styles',0|1
% 		a boolean indicating whether line styles, markers and colors shall be exported (default 1).
% - 'axes',0|1
% 		a boolean indicating whether axis ticks, tick labels and limits shall be exported (default 0).
% - 'maxpoints',100000
%       an integer denoting the maximum number of points exported to tex. If the actual number is larger,
%       the data will be interpolated to 'maxpoints'. The interpolation assumes
%       parametric plots if x and y are not monotonically increasing.
% 
% See
%   http://tug.ctan.org/tex-archive/graphics/pgf/contrib/pgfplots/
% for details about pgfplots.
%
%
%
% Copyright Christian Feuersaenger 2008
%
% This script requires Matlab version 7.4 (or above).
parser = inputParser;

parser.addRequired( 'outfile', @(x) ischar(x) );
parser.addParamValue( 'fig', gcf, @(x) ishandle(x) );
parser.addParamValue( 'styles', 1, @(x) x==0 || x==1 );
parser.addParamValue( 'axes' , 0, @(x) x==0 || x==1 );
parser.addParamValue( 'maxpoints', 100000, @(x) isnumeric(x) );

parser.parse( varargin{:} );


fighandle = parser.Results.fig;

lineobjs = findobj(fighandle, 'Type', 'line' );
axesobj = findobj( fighandle, 'Type', 'axes' );

% As far as I know, 'scatter' and 'scatter3' produce groups of this class:
scatterobjs = findobj(fighandle, 'Type', 'hggroup' );
lineobjs = [ lineobjs scatterobjs ];

legendobj = findobj( fighandle, 'tag', 'legend' );
if length(legendobj) > 0 
	allchildsoflegend = [ findobj( legendobj ) ];
	lineobjs = setdiff( lineobjs, allchildsoflegend );
	axesobj = setdiff( axesobj, allchildsoflegend );
end

FID=fopen( parser.Results.outfile, 'w' );
assert( FID >= 0, [ 'could not open file ' parser.Results.outfile ' for writing' ] );

ENDL=sprintf('\n');
TAB=sprintf('\t');
fwrite( FID, [ ...
	'\begin{tikzpicture}%' ENDL ...
	'\begin{axis}'] );

xislog = 0;
yislog = 0;

if length(axesobj) > 0
	axis = axesobj(1);
	xlabel = get( get(axis, 'XLabel'), 'String');
	ylabel = get( get(axis, 'YLabel'), 'String');
	zlabel = get( get(axis, 'ZLabel'), 'String');
	xscale = get(axis,'XScale');
	yscale = get(axis,'YScale');

	axisoptions = {};
	if length(xlabel) > 0
		axisoptions = [ axisoptions [ 'xlabel={' xlabel '}'] ];
	end
	if length(ylabel) > 0
		axisoptions = [ axisoptions ['ylabel={' ylabel '}'] ];
	end
	if strcmp(xscale,'log')
		xislog=1;
		axisoptions = [ axisoptions ['xmode=log'] ];
	end
	if strcmp(yscale,'log')
		yislog = 1;
		axisoptions = [ axisoptions ['ymode=log'] ];
	end
	if parser.Results.axes
		for k = 'xy'
			L = get(gca, [ k 'Lim'] );
			axisoptions = [ axisoptions [ k 'min=' num2str(L(1)) ] ];
			axisoptions = [ axisoptions [ k 'max=' num2str(L(2)) ] ];
		end

		for k = 'xy'
			L = get(gca, [ k 'Tick'] );
			opt = [ k 'tick={' ];
			for q=1:length(L)
				if q>1
					opt = [opt ',' ];
				end
				opt = [opt num2str(L(q)) ];
			end
			opt = [ opt '}' ];
			axisoptions = [axisoptions opt ];
		end

	end


	axisoptstr = [];
	for i = 1:length(axisoptions)
		if i>1
			axisoptstr = [axisoptstr ',' ENDL TAB];
		end
		axisoptstr = [axisoptstr axisoptions{i}];
	end
	if length( axisoptstr ) 
		fwrite( FID, [ '[' ENDL TAB axisoptstr ']' ENDL ] );
	end
end
fwrite( FID, ENDL );

if length(legendobj) > 0 
	legentries = get(legendobj, 'String');
	if length(legentries) > 0
		legstr = ['\legend{%' ENDL TAB ];
		for i = 1:length(legentries)
			legstr = [ legstr legentries{i} '\\%' ENDL ];
			if i ~= length(legentries)
				legstr = [ legstr TAB ];
			end
		end
		legstr = [ legstr '}%' ENDL ];
		fwrite( FID, legstr );
	end
end

xpointformat = '%f';
ypointformat = '%f';
if xislog
	xpointformat = '%e';
end
if yislog
	ypointformat = '%e';
end

for i = 1:length(lineobjs)
	
	x = get(lineobjs(i), 'XData');
	y = get(lineobjs(i), 'YData');
	z = get(lineobjs(i), 'ZData');

	if size(x,1) > 1
		disp( ['line element ' num2str(i) ' skipped: size ' num2str(size(x)) ' not supported']);
	end
	if abs(max(z) > 0)
		disp( ['line element ' num2str(i) ' skipped: only 2d-plots supported up to now']);
	end

	if size(x,2) > parser.Results.maxpoints
		% we need to re-interpolate the data!
		q = find( diff(x) < 0 );
		if length(q) 
			% parametric plot  x(t), y(t), z(t). 
			% we assume t = 1:size(x,2)
			X = 1:parser.Results.maxpoints;
			x = interp1( 1:size(x,2),x,  X);
			y = interp1( 1:size(y,2),y,  X);
			z = interp1( 1:size(z,2),z,  X);

		else
			% a normal plot y(x):
			X = linspace( min(x), max(x), parser.Results.maxpoints );
			y = interp1( x,y, X );
			x = X;
		end
	end

	coordstr = [];
	for j = 1:size(x,2)
		coordstr = [coordstr  sprintf(['\t(' xpointformat ',\t' ypointformat ')\n'], x(j), y(j)) ];
	end

	addplotoptstr = [];
	if parser.Results.styles
		markOpts = {};
		mark = [];
		linestyle = [];
		color = [];

		C = matlabColorToPGFColor( get(lineobjs(i), 'Color') );
		if length(C)
			color = [ 'color=' C ];
		end

		L = get(lineobjs(i), 'LineStyle' );
		switch L
		case 'none'
			linestyle = 'only marks';
		case '-'
			linestyle = [];
		case ':'
			linestyle = 'densely dotted';
		case '-:'
			linestyle = 'dash pattern={on 2pt off 3pt on 1pt off 3pt}';
		case '--'
			linestyle = 'densely dashed';
		end

		M = get(lineobjs(i), 'Marker');
		switch M
		case '.'
			mark = '*';
			markOpts = [ markOpts 'scale=0.1' ];
		case 'o'
			mark = '*';
		case 'x'
			mark = 'x';
		case '+'
			mark = '+';
		case '*'
			mark = 'asterisk';
		case 'square'
			mark = 'square*';
		case 'diamond'
			mark = 'diamond*';
		case '^'
			mark = 'triangle*';
		case 'v'
			mark = 'triangle*';
			markOpts = [ markOpts 'rotate=180' ];
		case '<'
			mark = 'triangle*';
			markOpts = [ markOpts 'rotate=90' ];
		case '>'
			mark = 'triangle*';
			markOpts = [ markOpts 'rotate=270' ];
		case 'pentagramm'
			mark = 'pentagon*';
		case 'hexagram'
			mark = 'oplus*';
		end

		M = matlabColorToPGFColor( get(lineobjs(i), 'MarkerFaceColor') );
		if length(M)
			markOpts = [ markOpts ['fill=' M] ];
		end

		M = matlabColorToPGFColor( get(lineobjs(i), 'MarkerEdgeColor') );
		if length(M)
			markOpts = [ markOpts ['draw=' M] ];
		end

		if length(color)
			if length(addplotoptstr)
				addplotoptstr = [addplotoptstr ',' ];
			end
			addplotoptstr = [ addplotoptstr color ];
		end

		if length(linestyle)
			if length(addplotoptstr)
				addplotoptstr = [addplotoptstr ',' ];
			end
			addplotoptstr = [ addplotoptstr linestyle ];
		end

		if length(mark)
			if length(addplotoptstr)
				addplotoptstr = [addplotoptstr ',' ];
			end
			addplotoptstr = [ addplotoptstr [ 'mark=' mark ] ];

			if length(markOpts)
				markOptsStr = 'mark options={';
				for q = 1:length(markOpts)
					if q > 1
						markOptsStr = [markOptsStr ',' ];
					end
					markOptsStr = [ markOptsStr markOpts{q} ];
				end
				markOptsStr = [ markOptsStr '}' ];

				addplotoptstr = [ addplotoptstr ',' markOptsStr ];
			end
		end
		

		if length(addplotoptstr)
			addplotoptstr = [ '[' addplotoptstr ']' ];
		end

	end
	fwrite( FID, [ ...
		'\addplot' addplotoptstr ' plot coordinates {' ENDL coordstr '};' ENDL ] );

end


fwrite( FID, [ ...
	'\end{axis}' ENDL ...
	'\end{tikzpicture}%' ENDL ] );
fclose(FID);

end

function cstr = matlabColorToPGFColor( C )

if length(C) ~= 3 | ischar(C) & strcmp(C,'none'),				cstr = [];
elseif norm( C - [0   0   1 ], 'inf' ) < 1e-10,		cstr = 'blue';
elseif norm( C - [0   1   0 ], 'inf' ) < 1e-10,	  cstr = 'green';  
elseif norm( C - [1   0   0 ], 'inf' ) < 1e-10,	  cstr = 'red';    
elseif norm( C - [0   1   1 ], 'inf' ) < 1e-10,	  cstr = 'cyan';   
elseif norm( C - [1   0   1 ], 'inf' ) < 1e-10,	  cstr = 'magenta';
elseif norm( C - [1   1   0 ], 'inf' ) < 1e-10,	  cstr = 'yellow'; 
elseif norm( C - [0   0   0 ], 'inf' ) < 1e-10,	  cstr = 'black';  
elseif norm( C - [1   1   1 ], 'inf' ) < 1e-10,	  cstr = 'white';  
else
	cstr= 'blue'; % FIXME
% cstr = [ '{rgb:red,' num2str( floor( C(1)*100) ) ';green,' num2str(floor(C(2)*100)) ';blue,' num2str(floor(C(3)*100)) '}' ];
end

end