
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  Title: StatisCLAS: A Package of Classification Methods   %%%%
%%%%  Author: David Casado de Lucas                            %%%%
%%%%  Version: 29th May 2013                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PURPOSE: This function generates, given a matrix, the LaTeX code of a table.

% INPUT VARIABLES:
%    matrix : Data matrix. Odd columns contain the means. When the dispersion is included, even columns contain it.
%    isDispersion : Logical variable to indicate whether or not even columns contain the dispersion.
%    caption : Caption of the table.
%    textSize : Size of the numbers, in the LaTeX sintaxis.
%    tabSpaces : If desired, number of spaces at the beginning of each line of the output, in case the user is indenting its LaTeX code.

% COMMENTS:
%   (1) Generic names 'C0', 'C1'... and 'F1', 'F2'... are used. Later, the user can find and replace them.
%   (2) This function can be used outside the framework of this paper.

function toLaTeXmatrix(matrix, isDispersion, caption, textSize, tabSpaces)

if ~exist('tabSpaces','var')
    tabSpaces = 0;
end
if ~exist('textSize','var')
    textSize = 'normalsize';
end
lineBeginning = [];
for i1 = 1:tabSpaces
    lineBeginning = [lineBeginning, ' '];
end
[rN cN] = size(matrix);
if isequal(isDispersion,'true')
    numCols = cN/2;
else
    numCols = cN;
end
disp(' '), disp(' ')
disp([lineBeginning, '\begin{table}'])
if exist('caption','var')
    disp([lineBeginning, '  \caption{', caption, '}'])
end
disp([lineBeginning, '  \begin{center}'])
def = '|l|';
for i2 = 1:numCols
   def = [def, 'c|'];
end
disp([lineBeginning, '    \begin{tabular}{', def, '}'])
disp([lineBeginning, '     \hline'])
headline = ['    ', ['\textbf{C', int2str(0), '}']];
for i2 = 1:numCols
    headline = [headline, ' & ', ['\textbf{', 'C', int2str(i2), '}']];
end
headline = [headline, '\\'];
disp([lineBeginning, '  ', headline])
for i1 = 1:rN
  newLine = ['   \textbf{', 'F', int2str(i1), '}'];
  for i2 = 1:cN
    if isequal(isDispersion,'true')
      if isequal(mod(i2,2),0)
        newLine = [newLine, ' \', textSize, '{(', num2str(matrix(i1,i2),'%6.4f'), ')}'];
      else
        newLine = [newLine, ' & \', textSize, '{', num2str(matrix(i1,i2),'%6.3f'), '}'];
      end
    else
      newLine = [newLine, ' & \', textSize, '{', num2str(matrix(i1,i2),'%6.3f'), '}'];
    end
  end
  newLine = [newLine, '\\'];
  disp([lineBeginning, '     \hline'])
  disp([lineBeginning, '   ',newLine])
end
disp([lineBeginning, '     \hline'])
disp([lineBeginning, '    \end{tabular}'])
disp([lineBeginning, '  \end{center}'])
disp([lineBeginning, '\end{table}'])
disp(' '), disp(' ')

end
