## Copyright (C) 2004, 2009 M. Koehler ## Copyright (C) 2009 VZLU Prague ## ## This program is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by the ## Free Software Foundation; either version 3 of the License, or (at your ## option) any later version. ## ## This program is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General ## Public License for more details. ## ## You should have received a copy of the General Public License along with ## this program. If not, see . ## ## original author: M. Koehler ## improved by: Jaroslav Hajek % Usage: % matrix2latex(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!!! function matrix2latex(matrix, filename, varargin) rowLabels = []; colLabels = []; alignment = 'l'; format = []; rowformats = []; colformats = []; textsize = []; rowsep = ':'; colsep = ':'; if (rem(nargin,2) == 1 || nargin < 2) error('matrix2latex: ', 'Incorrect number of arguments to %s.', mfilename); end okargs = {'rowlabels','columnlabels', 'alignment', 'format', ... 'rowformats', 'columnformats', 'size', 'rowseparators','columnseparators'}; 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 % rowformats rowformats = cellstr (lower(pval)); case 6 % rowformats colformats = cellstr (lower(pval)); case 7 % size textsize = pval; case 8 % row separators (logical) rowsep = pval; case 9 % column separators (logical) colsep = pval; end end end if (isempty (rowformats) + isempty (colformats) + isempty (format) < 2) error ("only one option of rowformats|columnformats|format can be given"); endif if (ischar (filename)) fid = fopen(filename, 'w'); else fid = filename; endif width = size(matrix, 2); height = size(matrix, 1); if isnumeric(matrix) if (! isempty (rowformats)) fmt = rowformats(:).'; if (length (fmt) != rows (matrix)) error ("rowformats does not match number of rows"); endif fmt(2,:) = "\n"; fmt = horzcat (fmt{:}); s = sprintf (fmt, matrix)(1:end-1); matrix = reshape (strsplit (s, "\n"), size (matrix)); elseif (! isempty (colformats)) fmt = colformats(:).'; if (length (fmt) != columns (matrix)) error ("colformats does not match number of columns"); endif matrix = matrix.'; fmt(2,:) = "\n"; fmt = horzcat (fmt{:}); s = sprintf (fmt, matrix)(1:end-1); matrix = reshape (strsplit (s, "\n"), size (matrix)).'; else if (isempty (format)) format = "%f"; endif s = sprintf ([format, "\n"], matrix)(1:end-1); matrix = reshape (strsplit (s, "\n"), size (matrix)); endif endif if(~isempty(textsize)) fprintf(fid, '\\begin{%s}', textsize); end fprintf(fid, '\\begin{tabular}{|'); if(~isempty(rowLabels)) fprintf(fid, 'l|'); end sep(1:width-1) = {''}; sep(colsep) = {'|'}; sep(width) = '|'; for i=1:width fprintf(fid, '%c%c', alignment, sep{i}); endfor fprintf(fid, '}\n'); fprintf(fid, '\\hline\n'); if(~isempty(colLabels)) if(~isempty(rowLabels)) fprintf(fid, '&'); end fprintf(fid, '\\textbf{%s}&', colLabels{1:width-1}); fprintf(fid, '\\textbf{%s}\\\\\\hline\n', colLabels{width}); end sep(1:height-1) = {''}; sep(rowsep) = {'\hline'}; sep(height) = '\hline'; for h=1:height if(~isempty(rowLabels)) fprintf(fid, '\\textbf{%s}&', rowLabels{h}); end fprintf(fid, '%s&', matrix{h, 1:width-1}); fprintf(fid, '%s\\\\%s\n', matrix{h, width}, sep{h}); end fprintf(fid, '\\end{tabular}\n'); if(~isempty(textsize)) fprintf(fid, '\\end{%s}', textsize); end if (ischar (filename)) fclose (fid); endif