function  print_xtab(counts,vals,fid)

%PRINT_XTAB -- Prints one- and two-way contingency tables.
%
% print_xtab(counts,vals)
% print_xtab(counts,vals,fid)
%
% COUNTS and VALS must be produced by XTAB1 or XTAB2 (or XTAB).
% FID must be a 'file id' of an open text file (see FOPEN). When not
% supplied, it defaults to 1 (that is, the standard output).
%
% See also XTAB1, XTAB2, XTAB, TABULATE, HIST, UNIQUE.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2002/01/17 20:32 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

if (nargin<3)
   fid = 1 ;     % print to standard output by default
end

if  (isnumeric(vals))       % shorthand VALS? (see XTAB1)
   print_oneway(counts,vals,fid) ;   
elseif  (~iscell(vals))
   error('VALS must be a cell array (or a column-vector), see XTAB.') ;
elseif  (length(vals)==1)
   print_oneway(counts,vals{1},fid) ;   
elseif  (length(vals)==2)
   print_twoway(counts,vals{1},vals{2},fid) ;   
else
   disp('I can only print one- and two-way contingency tables.') ;
end

%% Return no value
%%%% End of the main function


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  print_oneway(counts,vals,fid)

K = size(vals,2) ;            % number of columns
cum_cnt = cumsum(counts) ;
N100 = 100 / cum_cnt(end) ;   % 100 / N
pct = counts .* N100  ;
cum_pct = cum_cnt .* N100 ;

fprintf(fid,['\n',repmat(' ',1,8*K-5),...
        'Value   Count  Percent  Cum_cnt  Cum_pct\n']) ;
fprintf(fid,[repmat('-',1,8*K+35),'\n']) ;
fprintf(fid,[repmat(' %7g',1,K),'   %5d   %6.2f    %5d   %6.2f\n'],...
        [vals counts pct cum_cnt cum_pct]') ;
fprintf(fid,[repmat('-',1,8*K+35),'\n\n']) ;

%%%% End of PRINT_ONEWAY


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  print_twoway(counts,vals1,vals2,fid)

[n1,K1] = size(vals1) ;
[n2,K2] = size(vals2) ;

margin1 = sum(counts,2)' ;
margin2 = sum(counts) ;
N = sum(margin1) ;
pct1 = margin1 .* (100/N) ;
pct2 = margin2 .* (100/N) ;

%- Print the header
for i2 = 1:K2
  fprintf(fid,['\n',repmat(' ',1,8*K1),' |',repmat('%6g',1,n2),' |'],...
          vals2(:,i2)) ;
  if (i2==K2) fprintf(fid,' Total Percent\n') ; end
end
separator = [repmat('-',1,8*K1+6*n2+18),'\n'] ;
separator([8*K1+2,8*K1+6*n2+4]) = '+' ;
fprintf(fid,separator) ;
%- Print the body
for i1 = 1:n1
  fprintf(fid,[repmat(' %7g',1,K1),' |'],vals1(i1,:)) ;
  for i2=1:n2 
     if (counts(i1,i2)==0)
        fprintf(fid,'      ') ;
     else
        fprintf(fid,'%6d',counts(i1,i2));
     end
  end
  fprintf(fid,' |%6d  %6.1f\n',margin1(i1),pct1(i1)) ;
end
%- Print the footer
fprintf(fid,separator) ;
fprintf(fid,[repmat(' ',1,8*K1-7),'Total   |',repmat('%6d',1,n2)],...
        margin2) ;
fprintf(fid,' |%6d\n',N) ;
fprintf(fid,[repmat(' ',1,8*K1-7),'Percent |',repmat(' %5.1f',1,n2)],...
        pct2) ;
fprintf(fid,' |         100.0\n\n') ;

%%%% End of PRINT_TWOWAY

%%%%% End of file PRINT_XTAB.M
