-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2latex.m
156 lines (139 loc) · 5.12 KB
/
matrix2latex.m
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
function matrix2latex(matrix, filename, varargin)
% function: matrix2latex(...)
% Author: M. Koehler
% Contact: [email protected]
% Version: 1.1
% Date: May 09, 2004
% This software is published under the GNU GPL, by the free software
% foundation. For further reading see: http://www.gnu.org/licenses/licenses.html#GPL
% Usage:
% matrix2late(matrix, filename, varargs)
% where
% - matrix is a 2 dimensional numerical or cell array
% - filename is a valid filename, in which the resulting latex code will
% be stored
% - varargs is one ore more of the following (denominator, value) combinations
% + 'rowLabels', array -> Can be used to label the rows of the
% resulting latex table
% + 'columnLabels', array -> Can be used to label the columns of the
% resulting latex table
% + 'alignment', 'value' -> Can be used to specify the alginment of
% the table within the latex document. Valid arguments are: 'l', 'c',
% and 'r' for left, center, and right, respectively
% + 'format', 'value' -> Can be used to format the input data. 'value'
% has to be a valid format string, similar to the ones used in
% fprintf('format', value);
% + 'size', 'value' -> One of latex' recognized font-sizes, e.g. tiny,
% HUGE, Large, large, LARGE, etc.
%
% Example input:
% matrix = [1.5 1.764; 3.523 0.2];
% rowLabels = {'row 1', 'row 2'};
% columnLabels = {'col 1', 'col 2'};
% matrix2latex(matrix, 'out.tex', 'rowLabels', rowLabels, 'columnLabels', columnLabels, 'alignment', 'c', 'format', '%-6.2f', 'size', 'tiny');
%
% The resulting latex file can be included into any latex document by:
% /input{out.tex}
%
% Enjoy life!!!
rowLabels = [];
colLabels = [];
alignment = 'l';
format = [];
textsize = [];
if (rem(nargin,2) == 1 || nargin < 2)
error('matrix2latex: ', 'Incorrect number of arguments to %s.', mfilename);
end
okargs = {'rowlabels','columnlabels', 'alignment', 'format', 'size'};
for j=1:2:(nargin-2)
pname = varargin{j};
pval = varargin{j+1};
k = strmatch(lower(pname), okargs);
if isempty(k)
error('matrix2latex: ', 'Unknown parameter name: %s.', pname);
elseif length(k)>1
error('matrix2latex: ', 'Ambiguous parameter name: %s.', pname);
else
switch(k)
case 1 % rowlabels
rowLabels = pval;
if isnumeric(rowLabels)
rowLabels = cellstr(num2str(rowLabels(:)));
end
case 2 % column labels
colLabels = pval;
if isnumeric(colLabels)
colLabels = cellstr(num2str(colLabels(:)));
end
case 3 % alignment
alignment = lower(pval);
if alignment == 'right'
alignment = 'r';
end
if alignment == 'left'
alignment = 'l';
end
if alignment == 'center'
alignment = 'c';
end
if alignment ~= 'l' && alignment ~= 'c' && alignment ~= 'r'
alignment = 'l';
warning('matrix2latex: ', 'Unkown alignment. (Set it to \''left\''.)');
end
case 4 % format
format = lower(pval);
case 5 % format
textsize = pval;
end
end
end
fid = fopen(filename, 'w');
width = size(matrix, 2);
height = size(matrix, 1);
if isnumeric(matrix)
matrix = num2cell(matrix);
for h=1:height
for w=1:width
if(~isempty(format))
matrix{h, w} = num2str(matrix{h, w}, format);
else
matrix{h, w} = num2str(matrix{h, w});
end
end
end
end
if(~isempty(textsize))
fprintf(fid, '\\begin{%s}', textsize);
end
fprintf(fid, '\\begin{tabular}{|');
if(~isempty(rowLabels))
fprintf(fid, 'l|');
end
for i=1:width
fprintf(fid, '%c|', alignment);
end
fprintf(fid, '}\r\n');
fprintf(fid, '\\hline\r\n');
if(~isempty(colLabels))
if(~isempty(rowLabels))
fprintf(fid, '&');
end
for w=1:width-1
fprintf(fid, '\\textbf{%s}&', colLabels{w});
end
fprintf(fid, '\\textbf{%s}\\\\\\hline\r\n', colLabels{width});
end
for h=1:height
if(~isempty(rowLabels))
fprintf(fid, '\\textbf{%s}&', rowLabels{h});
end
for w=1:width-1
fprintf(fid, '%s&', matrix{h, w});
end
fprintf(fid, '%s\\\\\\hline\r\n', matrix{h, width});
end
fprintf(fid, '\\end{tabular}\r\n');
if(~isempty(textsize))
fprintf(fid, '\\end{%s}', textsize);
end
fclose(fid);