]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/csv2latex.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / csv2latex.m
1 ## Copyright (C) 2006 Hansgeorg Schwibbe
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## Creates a latex file from a csv file. The generated latex file contains a 
17 ## tabular with all values of the csv file. The tabular can be decorated with 
18 ## row and column titles. The generated latex file can be inserted in any latex
19 ## document by using the '\input{latex file name without .tex}' statement.
20 ##
21 ## Usage: 
22 ##  - csv2latex(csv_file, csv_sep, latex_file)
23 ##  - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments)
24 ##  - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments, has_hline)
25 ##  - csv2latex(csv_file, csv_sep, latex_file,   
26 ##              tabular_alignments, has_hline, column_titles)
27 ##  - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments,
28 ##              has_hline, column_titles, row_titles)
29 ##
30 ## Parameters:
31 ##  csv_file - the path to an existing csv file
32 ##  csv_sep - the seperator of the csv values
33 ##  latex_file - the path of the latex file to create     
34 ##  tabular_alignments - the tabular alignment preamble (default = {'l','l',...})
35 ##  has_hline - indicates horizontal line seperator (default = false)
36 ##  column_titles - array with the column titles of the tabular (default = {})
37 ##  row_titles - array with the row titles of the tabular (default = {})
38 ##
39 ## Examples:
40 ##  # creates the latex file 'example.tex' from the csv file 'example.csv' 
41 ##  csv2latex("example.csv", '\t', "example.tex");
42 ##
43 ##  # creates the latex file with horizontal and vertical lines
44 ##  csv2latex('example.csv', '\t', 'example.tex', {'|l|', 'l|'}, true);
45 ## 
46 ##  # creates the latex file with row and column titles
47 ##  csv2latex('example.csv', '\t', 'example.tex', {'|l|', 'l|'}, true, 
48 ##            {'Column 1', 'Column 2', 'Column 3'}, {'Row 1', 'Row 2'});
49
50 function csv2latex (csv_file, csv_sep, latex_file, tabular_alignments, has_hline, column_titles, row_titles)
51
52   ## set up the default values
53   if nargin < 7
54    row_titles = {};
55   end
56   if nargin < 6
57    column_titles = {};
58   end
59   if nargin < 5
60    has_hline = false;
61   end
62   if nargin < 4
63    tabular_alignments = {};
64   end
65
66   ## load the csv file and create the csv cell
67   [fid, msg] = fopen (csv_file, 'r'); # open the csv file to read
68   csv = cell();
69   if fid != -1
70     [val, count] = fread(fid); # read all data from the file
71     fclose(fid); # close the csv file after reading
72     csv_value = '';
73     line_index = 1;
74     value_index = 1;
75     for index = 1:count
76       if val(index) == csv_sep
77         csv(line_index, value_index) = csv_value;
78         value_index++;
79         csv_value = '';
80       elseif (val(index) == '\n' || (val(index) == '\r' && val(index+1) == '\r'))
81         csv(line_index, value_index) = csv_value;
82         value_index++;
83         csv_value = '';
84         value_index = 1;
85         line_index++;
86       else
87         csv_value = sprintf('%s%c', csv_value, val(index));
88       end
89     end
90   end
91
92   ## get the size and length values
93   [row_size, col_size] = size(csv);
94   alignment_size = length(tabular_alignments);
95   column_title_size = length(column_titles);
96   row_title_size = length(row_titles);
97
98   ## create the alignment preamble and the column titles
99   alignment_preamble = '';
100   tabular_headline = '';
101   if row_title_size != 0
102     current_size = col_size + 1;
103   else
104     current_size = col_size;
105   end
106   for col_index = 1:current_size
107    if col_index <=  alignment_size
108      alignment_preamble = sprintf ('%s%s', alignment_preamble, tabular_alignments(col_index));
109    else
110      alignment_preamble = sprintf ('%sl', alignment_preamble);
111    end
112    if column_title_size != 0
113      if col_index <= column_title_size
114        if col_index == 1
115          tabular_headline = sprintf ('%s', column_titles(col_index));
116        else
117          tabular_headline = sprintf ('%s & %s', tabular_headline, column_titles(col_index));
118        end
119      else
120        tabular_headline = sprintf ('%s &', tabular_headline);       
121      end
122    end
123   end
124
125   ## print latex file
126   [fid, msg] = fopen (latex_file, 'w'); # open the latex file for writing
127   if fid != -1
128     fprintf (fid, '\\begin{tabular}{%s}\n', alignment_preamble); # print the begin of the tabular
129     if column_title_size != 0
130       if has_hline == true
131         fprintf (fid, '  \\hline\n');
132       end
133       fprintf (fid, '  %s \\\\\n',  tabular_headline); # print the headline of the tabular
134     end
135     for row_index = 1:row_size
136       if has_hline == true
137         fprintf (fid, '  \\hline\n');
138       end
139       for col_index = 1:col_size
140         if col_index == 1
141           if row_title_size != 0
142             if row_index <= row_title_size
143               fprintf (fid, '  %s & ', row_titles(row_index)); # print the row title
144             else
145               fprintf (fid, '  & '); # print an empty row title
146             end
147           end
148           fprintf (fid, '  %s ', csv{row_index, col_index});
149         else
150          fprintf (fid, '& %s ', csv{row_index, col_index});
151         end
152       end
153       fprintf (fid, '\\\\\n');
154     end
155     if has_hline == true
156       fprintf (fid, '  \\hline\n');
157     end
158     fprintf (fid, '\\end{tabular}', alignment_preamble); # print the end of the tabular
159     fclose(fid); # close the latex file after writing
160   end
161 end