function tab=mattab1(A,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,title)
%MATTAB1   Conversion of MATLAB matrices to tables.
%          tab=mattab1(A,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,
%          title) converts a numerical (lxm) matrix A to a table (i.e. a
%          string matrix with labels and borders included).  The
%          recommeded use of this file is to improve printouts of results.
%          For final results, use TEXTAB1 which generates tables to be
%          read by TEX.
%
%          If ntype=0, n decimal places will be displayed.  Otherwise, n 
%          significant digits will be displayed.  (Note: this holds for 
%          all elements of A.)  The form that the table can take is:
%
%          ---------------------------------------------------------------
%          |rlabel1(1,:)|                   clabel1                      |
%          |            |-------------------------------------------------
%          |rlabel1(2,:)|       clabel2(1,:)       |clabel2(2,:)|  ...   |
%          |            |-------------------------------------------------
%          |rlabel1(3,:)| clabel3(1,:)|clabel3(2,:)|clabel3(3,:)|  ...   |
%          |--------------------------------------------------------------
%          |rlabel2(1,:)|    A(1,1)   |   A(1,2)   |   A(1,3)   |  ...   |
%          ---------------------------------------------------------------
%          |rlabel2(2,:)|    A(2,1)   |   A(2,2)   |   A(2,3)   |  ...   |
%          ---------------------------------------------------------------
%          |rlabel2(3,:)|    A(3,1)   |   A(3,2)   |   A(3,3)   |  ...   |
%          ---------------------------------------------------------------
%          |    :              :             :            :         :    |
%          ---------------------------------------------------------------
%                              *** title is put here ***
%
%          where 
%
%            clabel1 = a row string used in the top label which will span 
%                      all columns of A, e.g.  'The Header'
%            clabel2 = a string matrix with m or fewer (since A is l x m) 
%                      rows, the first column of each row being a character, 
%                      '1','2', ..,'8', or '9', telling how many columns 
%                      of A the corresponding label will span, e.g.
%                      clabel2=['2  cols 1,2 ';   Note: the labels will
%                               '3 cols 3,4,5']   appear without the
%                                                 first '2' and '3'
%            clabel3 = a string matrix with m rows used in the bottom
%                      column labels
%            rlabel1 = a string matrix with 3 rows to be used for 
%                      labeling the rows
%            rlabel2 = a string matrix with l rows corresponding to
%                      rows of A
%            title   = a string with the title for the table
%
%          NOTE: If any of the row or column labels are not desired, set 
%          the option to 0.  For example, if rlabel1=0 and rlabel2=0,
%          there will only be labels for the columns.
%
%          Textab1 returns the matrix string tab.
%
%          See also MATTAB2, TEXTAB1, TEXTAB2.
%

%          Ellen R. McGrattan, 10-13-87
%          Revised, 2-24-89,  ERM
%
[l,m]=size(A);
if max(max(imag(A)~=zeros(l,m)));
  disp('Warning: complex number encountered. Code needs updating for this.');
end;
if isstr(rlabel2);
  [srow,scol]=size(rlabel2);
  if srow<l; error('The second set of row labels have too few rows'); end;
  if srow>l; rlabel2=rlabel2(1:l,:); end;
  if ~isstr(rlabel1); rlabel1=[' ';' ';' ']; end;
else;
  if isstr(rlabel1);
    rlabel2=setstr(32*ones(l,1));
  end;
end;
if isstr(rlabel1);
  [srow,scol]=size(rlabel1);
  x=isstr(clabel1)+isstr(clabel2)+isstr(clabel3);
  if srow< x;
    error('The first set of row labels have too few rows')
  end;
  if x==0; rlabel1=0; else; rlabel1=rlabel1(1:x,:); end;
end;
if isstr(clabel2);
  [srow,scol]=size(clabel2);
  no2=[ ];
  for i=1:srow;
    eval(['no2=[no2;',clabel2(i,1),'];'])
  end;
  sum2=sum(no2);
  if sum2<m; 
    error('The second set of column labels have too few rows.')
  end;
  if sum2>m;
    disp('WARNING: Not all of your second column labels are being used.')
    x=[ ];
    for i=1:srow
      y=sum(no2(1:i));
      x=[x;clabel2(i,:)];
      if y==m;
        break;
      elseif y>m;
        no2(i)=m-sum(no2(1:i-1));
        break;
      end;
    end;
    clabel2=x;
    [srow,scol]=size(clabel2);
    no2=no2(1:srow);
  end;
  clabel2=clabel2(:,2:scol);
else;
  no2=m;
end;

if ntype;
  a=abs(A);
  a=(a==0)+(a~=0).*a;
  B=floor(log10(a));
  for i=1:l;
    for j=1:m;
      x=10^B(i,j)/10^(n-1);
      C(i,j)=round(A(i,j)/x)*x;
    end;
  end;
  c=abs(C);
  c=(c==0)+(c~=0).*c;
  B=floor(log10(c));
  for i=1:l;
    for j=1:m;
      if B(i,j)-n+1>=0;
        D(i,j)=1;
      elseif B(i,j)<0;
        D(i,j)=-1;
      else;
        D(i,j)=0;
      end;
    end;
  end;
  E=(10).^(-B+n-1).*C;
  str=' ';
  for i=1:l;
    for j=1:m;
      if D(i,j)==1; 
        new=[' ',int2str(C(i,j))];
      elseif D(i,j)==0;
        x=10^(n-1-B(i,j))*(C(i,j)-fix(C(i,j)));
        count=0;
        for k=1:n-2-B(i,j);
          if abs(x)<10^k; count=count+1; end;
        end;
        new=[' ',int2str(fix(C(i,j))),'.',setstr(ones(1,count)*48), ...
             int2str(abs(x)),' '];
      else;
        if A(i,j)>=0;
          new=[' 0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j))),' '];
        else;
          new=[' -0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j))),' '];
        end;
      end;
      [srow,scol]=size(str);
      [nrow,ncol]=size(new);
      str=[setstr(32*ones(srow,ncol-scol)),str;
           setstr(32*ones(1,scol-ncol)),new];
    end;
  end;
  str=str(2:l*m+1,:);
else;
  B=fix(10^n*A+.5*sign(A))*10^(-n);
  C=abs(fix(B));
  D=abs(round(10^n*(B-fix(B))));
  str=' ';
  for i=1:l;
    for j=1:m;
      sc=int2str(D(i,j));
      x=length(sc);
      if x<n; sc=[setstr(48*ones(1,n-x)),sc]; end;
      if A(i,j)>=0; 
        new=[' ',int2str(C(i,j)),'.',sc,' '];
      else;
        new=[' -',int2str(C(i,j)),'.',sc,' '];
      end;
      [srow,scol]=size(str);
      [nrow,ncol]=size(new);
      str=[setstr(32*ones(srow,ncol-scol)),str;
           setstr(32*ones(1,scol-ncol)),new];
    end;
  end;
  str=str(2:l*m+1,:);
end;
flag=0;
[srow,scol]=size(str);

if isstr(clabel3);
  [row3,col3]=size(clabel3);
  if col3<scol;
    label=clabel3;
    tem1=row3;
  elseif col3>scol;
    label=str;
    tem1=srow;
  end;
  diff=abs(col3-scol);
  if rem(diff,2);
    if flag==0;
      flag=-1;
      label=[setstr(32*ones(tem1,(diff-1)/2+1)),label, ...
             setstr(32*ones(tem1,(diff-1)/2))];
    else;
      flag=0;
      label=[setstr(32*ones(tem1,(diff-1)/2)),label, ...
             setstr(32*ones(tem1,(diff-1)/2+1))];
    end;
  else;
    tem2=setstr(32*ones(tem1,diff/2));
    label=[tem2,label,tem2];
  end;
  if col3<scol; 
    clabel3=label; 
  elseif col3>scol; 
    str=label; 
  end;
end;
[srow,scol]=size(str);

if isstr(clabel2);
  [row2,col2]=size(clabel2);
  best(1)=0;
  for i=1:row2;
    if col2>no2(i)*scol+no2(i)-1;
      diff=col2-no2(i)*scol-no2(i)+1;
      if diff>best(1); best(1)=diff; best(2)=i; end;
    end;
  end;  
  diff=ceil(best(1)/no2(best(2)));    
  if diff>0;
    if rem(diff,2);
      if flag==0;
        flag=-1;
        str=[setstr(32*ones(l*m,(diff-1)/2+1)),str, ...
             setstr(32*ones(l*m,(diff-1)/2))];
        if isstr(clabel3);
          [row3,col3]=size(clabel3);
          clabel3=[setstr(32*ones(row3,(diff-1)/2+1)),clabel3, ...
                   setstr(32*ones(row3,(diff-1)/2))];
        end;
      else;
        flag=0;
        str=[setstr(32*ones(l*m,(diff-1)/2)),str, ...
             setstr(32*ones(l*m,(diff-1)/2+1))];
        if isstr(clabel3);
          [row3,col3]=size(clabel3);
          clabel3=[setstr(32*ones(row3,(diff-1)/2)),clabel3, ...
                   setstr(32*ones(row3,(diff-1)/2+1))];
        end;
      end;
    else;
      tem1=setstr(32*ones(l*m,diff/2));
      str=[tem1,str,tem1];
      if isstr(clabel3); 
        [row3,col3]=size(clabel3);
        tem1=setstr(32*ones(row3,diff/2));
        clabel3=[tem1,clabel3,tem1]; 
      end;
    end;
  end;
end;
[srow,scol]=size(str);
if isstr(clabel1);
  [row1,col1]=size(clabel1);
  if col1>m*scol+m-1;
    diff=ceil((col1-m*scol-m+1)/m);
    if rem(diff,2);
      if flag==0;
        flag=-1;
        str=[setstr(32*ones(l*m,(diff-1)/2+1)),str, ...
             setstr(32*ones(l*m,(diff-1)/2))];
        if isstr(clabel3);
          [row3,col3]=size(clabel3);
          clabel3=[setstr(32*ones(row3,(diff-1)/2+1)),clabel3, ...
                   setstr(32*ones(row3,(diff-1)/2))];
        end;
      else;
        flag=0;
        str=[setstr(32*ones(l*m,(diff-1)/2)),str, ...
             setstr(32*ones(l*m,(diff-1)/2+1))];
        if isstr(clabel3);
          [row3,col3]=size(clabel3);
          clabel3=[setstr(32*ones(row3,(diff-1)/2)),clabel3, ...
                   setstr(32*ones(row3,(diff-1)/2+1))];
        end;
      end;
    else;
      tem1=setstr(32*ones(l*m,diff/2));
      str=[tem1,str,tem1];
      if isstr(clabel3); 
        [row3,col3]=size(clabel3);
        tem1=setstr(32*ones(row3,diff/2));
        clabel3=[tem1,clabel3,tem1]; 
      end;
    end;
  end;
  [srow,scol]=size(str);
  if col1<m*scol+m-1;
    diff=m*scol+m-1-col1;
    if rem(diff,2);
      clabel1=[setstr(32*ones(1,(diff-1)/2+1)),clabel1, ...
           setstr(32*ones(1,(diff-1)/2))];
    else;
      tem1=setstr(32*ones(1,diff/2));
      clabel1=[tem1,clabel1,tem1];
    end;
  end; 
end;
[srow,scol]=size(str);
width=m*scol+m+1;
tab=setstr(32*ones(2*l+1,width));
for i=1:l;
  tab(2*i-1,:)=setstr(45*ones(1,width));
  for j=1:m;
    tab(2*i,(j-1)*(scol+1)+1:j*(scol+1))=['|',str((i-1)*m+j,:)];
  end;
  tab(2*i,width)='|';
end;

tab(2*l+1,:)=setstr(45*ones(1,width));
if isstr(clabel1) | isstr(clabel2) | isstr(clabel3);
  tab=[setstr(45*ones(1,width));tab];
end;
if isstr(clabel3);
  tab=[setstr(32*ones(1,width));tab];
  for i=1:m;
    tab(1,(i-1)*(scol+1)+1:i*(scol+1))=['|',clabel3(i,:)];
  end;
  tab(1,width)='|';
  tab=[setstr(45*ones(1,width));tab];
end;
if isstr(clabel2);
  tab=[setstr(32*ones(1,width));tab];
  for i=1:row2;
col2
no2(i)
scol
    diff=no2(i)*scol+no2(i)-1-col2
    if rem(diff,2);
      tab(1,sum(no2(1:i-1))*(scol+1)+1:sum(no2(1:i))*(scol+1))= ...
          ['|',setstr(32*ones(1,(diff-1)/2+1)),clabel2(i,:),  ...
          setstr(32*ones(1,(diff-1)/2))]
    else;
      tem1=setstr(32*ones(1,diff/2))
      tab(1,sum(no2(1:i-1))*(scol+1)+1:sum(no2(1:i))*(scol+1))= ...
          ['|',tem1,clabel2(i,:),tem1]
    end;
  end;
  tab(1,width)='|';
  tab=[setstr(45*ones(1,width));tab];
end;
if isstr(clabel1);
  tab=[setstr(45*ones(1,width));'|',clabel1,'|';tab];
end;
if isstr(rlabel1);
  [row1,col1]=size(rlabel1);
  [row2,col2]=size(rlabel2);
  if col1>col2;
    diff=col1-col2;
    if rem(diff,2);
      rlabel2=[setstr(32*ones(row2,(diff-1)/2+1)),rlabel2, ...
               setstr(32*ones(row2,(diff-1)/2))]
    else;
      tem1=setstr(32*ones(row2,diff/2));
      rlabel2=[tem1,rlabel2,tem1];
    end;
  elseif col2>col1;
    diff=col2-col1;
    if rem(diff,2);
      rlabel1=[setstr(32*ones(row1,(diff-1)/2+1)),rlabel1, ...
               setstr(32*ones(row1,(diff-1)/2))];
    else;
      tem1=setstr(32*ones(row1,diff/2));
      rlabel1=[tem1,rlabel1,tem1];
    end;
  end;
  [row1,col1]=size(rlabel1);
  str=[setstr(45*ones(1,col1+1))];
  for i=1:row1-1;
    str=[str;'|',rlabel1(i,:);'|',setstr(32*ones(1,col1))];
  end;
  str=[str;'|',rlabel1(row1,:);setstr(45*ones(1,col1+1))];
  for i=1:l;
    str=[str;setstr(45*ones(1,col1+1));'|',rlabel2(i,:)];
  end;
  str=[str;setstr(45*ones(1,col1+1))];
  tab=[str,tab];
elseif isstr(rlabel2);
  [row2,col2]=size(rlabel2);
  str=[ ];
  for i=1:l;
    str=[str;setstr(45*ones(1,col2+1));'|',rlabel2(i,:)];
  end;
  str=[str;setstr(45*ones(1,col2+1))];
  tab=[str,tab];
end;

return
if isstr(clabel3);
  str=[clabel3;str];
end;
if isstr(clabel1);
  str=[clabel1;str];
end;

tab=str;
return
tab=tab(2:l+1,:);
[srow,scol]=size(tab);
if isstr(rlabel2);
  x=abs('\strut&');
  tab=[setstr(ones(srow,1)*x),rlabel2,tab];
else;
  x=abs('\strut');
  tab=[setstr(ones(srow,1)*x),tab(:,2:scol)];
end;
[srow,scol]=size(tab);
[hrow,hcol]=size(header);
tab=[header,setstr(32*ones(hrow,scol-hcol));
     tab,setstr(32*ones(srow,hcol-scol))];
str=['\centerline{',title,'}'];
[hrow,hcol]=size(tab);
[srow,scol]=size(str);
x=max(hcol,scol);
tab=[tab,setstr(32*ones(hrow,scol-hcol));
     '\medskip',setstr(32*ones(1,x-8));
     str,setstr(32*ones(1,hcol-scol));
     '\vfill\eject',setstr(32*ones(1,x-12));
     '\bye',setstr(32*ones(1,x-4))];





if isstr(clabel1);
  header=[header;'\strut&                                           '];
  if isstr(rlabel1);
    str=['      ',rlabel1(1,:),'&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \multispan{',int2str(2*m-1),'}\hfil ',clabel1,'\hfil&\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
  if isstr(rlabel1);
    [hrow,hcol]=size(header);
    header=[header;'      &&',setstr(32*ones(1,hcol-8))];
  end;
  str=['      \multispan{',int2str(2*m+1),'}\widehrulefill\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;
if isstr(clabel2);
  [hrow,hcol]=size(header);
  header=[header;'\strut&',setstr(32*ones(1,hcol-7))];
  if isstr(rlabel1);
    if isstr(clabel1);
      str=['      ',rlabel1(2,:),'&&'];
    else;
      str=['      ',rlabel1(1,:),'&&'];
    end;
    [srow,scol]=size(str);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;

  x=length(no2);
  for i=1:x-1;
    str=['      \multispan{',int2str(2*no2(i)-1),'}\hidewidth ', ...
         clabel2(i,:),'\hidewidth&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \multispan{',int2str(2*no2(x)-1),'}\hidewidth ', ...
       clabel2(x,:),'\hidewidth&\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
  if isstr(rlabel1);
    [hrow,hcol]=size(header);
    header=[header;'      &&',setstr(32*ones(1,hcol-8))];
  end;
  str=['      \multispan{',int2str(2*m+1),'}\widehrulefill\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;

if isstr(clabel3);
  [hrow,hcol]=size(header);
  header=[header;'\strut&',setstr(32*ones(1,hcol-7))];
  if isstr(rlabel1);
    if (~isstr(clabel1) & ~isstr(clabel2));
      str=['      ',rlabel1(1,:),'&&'];
    elseif ((isstr(clabel1) & ~isstr(clabel2))  ...
          | (isstr(clabel2) & ~isstr(clabel1)));
      str=['      ',rlabel1(2,:),'&&'];
    else;
      str=['      ',rlabel1(3,:),'&&'];
    end;
    [srow,scol]=size(str);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;

  for i=1:m-1;
    str=['      \omit\hidewidth ',clabel3(i,:),'\hidewidth&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \omit\hidewidth ',clabel3(m,:),'\hidewidth&\cr\widetablerule'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;
