function tab=textab1(A,num,decorsig);
%TEXTAB1   Conversion of MATLAB tables to TEX tables.
%          tab=textab1(A,num,decorsig) converts a numerical table A in 
%          MATLAB to a table padded with the appropriate code for TEX 
%          files, (e.g. TABTYP<i>.TEX, located in subdirectory 
%          [MCGRATTAN.TEX].  If decorsig=1, then "num" decimal places
%          are used for reporting all values.  If decorsig=0, then
%          "num" significant digits are reported for all values in A.
%          Textab1 returns the matrix string tab.
%
%          See also TEXTAB2, TEXTAB3, TEXTAB4.
%

%          Ellen R. McGrattan, 10-13-87
%          Revised, 11-24-88,  ERM
%
[m,n]=size(A);
if max(max(imag(A)~=zeros(m,n)));
  disp('Warning: complex number encountered. Code needs updating for this.');
end;
if decorsig==1;
  SA=A;
  A=fix(10^num*A+.5*sign(A))*10^(-num);
  B=abs(fix(A));
  mxlr=n*(4+length(int2str(max(max(B))))+num);
  C=abs(round(10^num*(A-fix(A))));
  for i=1:m;
    strr='';
    for j=1:n;
      sc=int2str(C(i,j));
      if length(sc)<num;
        for k=1:num-length(sc);
          sc=['0',sc];
        end;
      end;
      if SA(i,j)>=0; 
        strr=[strr,'&& ',int2str(B(i,j)),'.',sc];
      else;
        strr=[strr,'&&-',int2str(B(i,j)),'.',sc];
      end;
    end;
    if length(strr)<mxlr;
      for k=1:mxlr-length(strr);
        strr=[strr,' '];
      end;
    end;
    if i<m; 
      tab=[tab;[strr,'&\cr\tablerule   ']];
    else;
      tab=[tab;[strr,'&\cr\tablerule}}}']];
    end;
  end;
else;
  a=abs(A);
  a=(a==0)+(a~=0).*a;
  B=floor(log10(a));
  for i=1:m;
    for j=1:n;
      x=10^B(i,j)/10^(num-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:m;
    for j=1:n;
      if B(i,j)-num+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+num-1).*C;
  tab=' ';
  for i=1:m;
    lst=length(tab);
    str='';
    for j=1:n;
      if D(i,j)==1; 
        str=[str,'&& ',int2str(C(i,j))];
      elseif D(i,j)==0;
        y=10^(num-1-B(i,j))*(C(i,j)-fix(C(i,j)));
        count=0;
        for k=1:num-2-B(i,j);
          if abs(y)<10^k; count=count+1; end;
        end;
        str=[str,'&& ',int2str(fix(C(i,j))),'.',setstr(ones(1,count)*48), ..
             int2str(abs(y))];
      else;
        if A(i,j)>=0;
          str=[str,'&& 0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j)))];
        else;
          str=[str,'&& -0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j)))];
        end;
      end;
    end;
    if i<m; 
      str=[str '&\cr\tablerule   '];
    else;
      str=[str,'&\cr\tablerule}}}'];
    end;
    ls=length(str);
    if ls>lst;
      tab=[tab,setstr(ones(i,ls-lst)*32);str];
    elseif ls<=lst;
      tab=[tab;str setstr(ones(1,lst-ls)*32)];
    end;
  end;
  tab=tab(2:m+1,:);
end;
function textab=textab2(M,S,ndec);
%TEXTAB2 converts numerical tables M and S to a table of the form:
%
%      [---------------------------------------]
%      [  M(1,1)  |  M(1,2)  |  ... |  M(1,n)  ]
%      [ (S(1,1)) | (S(1,2)) |  ... | (S(1,n)) ]
%      [---------------------------------------]
%      [  M(1,1)  |  M(1,2)  |  ... |  M(1,n)  ]
%      [ (S(1,1)) | (S(1,2)) |  ... | (S(1,n)) ]
%      [---------------------------------------]
%      [    :     |    :     |      |    :     ]
%      [    :     |    :     |      |    :     ]
%      [---------------------------------------]
%      [  M(1,1)  |  M(1,2)  |  ... |  M(1,n)  ]
%      [ (S(1,1)) | (S(1,2)) |  ... | (S(1,n)) ]
%      [---------------------------------------]
%
%  with extra code needed for using with tex files TABTYP<i>.TEX, 
%  located in subdirectory [MCGRATTAN.TEX].
%  
%  INPUTS: 
%    (i) M = matrix 1 for table (e.g. a matrix of means)
%   (ii) S = matrix 2 for table (e.g. a matrix of standard deviations)
%  (iii) ndec = number of decimal places to use for all values in table
% 
%  OUTPUT: textab = string to be read into one of the TABTYP<i>.TEX files
%
%  NOTES: see TEXTAB<j>.M, j=1,..4 for additional table choices
%
[m,n]=size(M);
if [m,n]~=size(S); error('The two matrices must have the same dimensions'); end;
SM=M;
SS=S;
if max(max(imag(M)~=zeros(m,n)))>0 | max(max(imag(S)~=zeros(m,n)))>0;
  disp('Warning: complex number encountered.  Code needs updating for this')
end;
M=fix(10^ndec*M+.5*sign(M))*10^(-ndec);
S=fix(10^ndec*S+.5*sign(S))*10^(-ndec);
A=abs(fix(M)); 
B=abs(fix(S));
mxlr=n*(6+length(int2str(max(max(max(A,B)))))+ndec);   
C=abs(round(10^ndec*(M-fix(M))));
D=abs(round(10^ndec*(S-fix(S))));
for i=1:m;
  strr1='';
  strr2='';
  for j=1:n;
    sc=int2str(C(i,j));
    if length(sc)<ndec;
      for k=1:ndec-length(sc);
        sc=['0',sc];
      end;
    end;
    sd=int2str(D(i,j));
    if length(sd)<ndec;
      for k=1:ndec-length(sd);
        sd=['0',sd];
      end;
    end;
    if SM(i,j)>=0; 
      strr1=[strr1,'&&  ',int2str(A(i,j)),'.',sc,' '];
    else;
      strr1=[strr1,'&& -',int2str(A(i,j)),'.',sc,' '];
    end;
    if SS(i,j)>=0; 
      strr2=[strr2,'&&( ',int2str(B(i,j)),'.',sd,')'];
    else;
      strr2=[strr2,'&&(-',int2str(B(i,j)),'.',sd,')'];
    end;
  end;
  if length(strr1)<mxlr;
    for k=1:mxlr-length(strr1);
      strr1=[strr1,' '];
    end;
  end;
  if length(strr2)<mxlr;
    for k=1:mxlr-length(strr2);
      strr2=[strr2,' '];
    end;
  end;
  if i<m; 
    textab=[textab;[strr1,'&\cr             '];[strr2,'&\cr\tablerule   ']];
  else;
    textab=[textab;[strr1,'&\cr             '];[strr2,'&\cr\tablerule}}}']];
  end;
end;
function textab=textab3(A,ndec,lab1,lab2);
%TEXTAB3 is an extension of TEXTAB1 and converts a numerical table 
%  A in matlab to a table padded with the appropriate code for tex files 
%  TABTYP<i>.TEX located in subdirectory [MCGRATTAN.TEX].  This
%  version of TEXTAB allows for labels in row 1, lab1, and labels 
%  in column 1, lab2.  In tex, you get a matrix of the form:
%
%   ------------------------------------------------------------------------
%   | lab1(1,:) || lab1(2,:) | lab1(3,:) | lab1(4,:) |  ...    |lab1(n+1,:)|
%   ________________________________________________________________________
%   ------------------------------------------------------------------------
%   | lab2(1,:) ||   A(1,1)  |   A(1,2)  |   A(1,3)  |  ...    |   A(1,n)  | 
%   ------------------------------------------------------------------------
%   | lab2(2,:) ||   A(2,1)  |   A(2,2)  |   A(2,3)  |  ...    |   A(2,n)  |
%   ------------------------------------------------------------------------
%   |           :                             :                       |
%   ------------------------------------------------------------------------
%   | lab2(m,:) ||   A(m,1)  |   A(m,2)  |   A(m,3)  |  ...    |   A(m,n)  |
%   ------------------------------------------------------------------------
%
%  
%  INPUTS: 
%    (i) A = an mxn matrix for table
%   (ii) ndec = number of decimal places to use for all values
%  (iii) lab1= a string matrix with n+1 rows if lab2~=0, n rows if lab2=0,
%              and an arbitrary number of columns.  If lab2~=0, row i of 
%              lab1 should contain a label for the (i-1)st column of A,
%              i=1,..n+1. If lab2=0, row i of lab1 should contain a label 
%              for the ith column of A, i=1,..n.
%              If lab1=0, column labels are not created.
%   (iv) lab2= a string matrix with m rows and an arbitrary number of 
%              columns.  Row i of lab2 should contain a label for the ith 
%              row of A.
%              If lab2=0, row labels are not created.
% 
%  OUTPUT: textab = string to be read into one of the TABTYP<i>.TEX files
%
%  NOTES: see TEXTAB<j>.M, j=1,..4, for additional table choices
%
if max(max(imag(A)~=zeros(m,n)));
  disp('Warning: complex number encountered. Code needs updating for this.');
end;
[m,n]=size(A);
[rl1,cl1]=size(lab1);
[rl2,cl2]=size(lab2);
case=1;                                       % neither row nor column labels
if lab1==0;                                   % row labels but no column labels
  if lab2~=0;
    case=2;
    if m~=rl2; error('Wrong number of rows in row label string'); end;
  end;
end;
if lab1~=0                                    % column labels but no row labels
  if lab2==0;
    case=3;
    if rl1~=n; error('Wrong number of rows in column label string'); end;
  end;
end;
if lab1~=0;                                   % both column and row labels
  if lab2~=0;
    case=4;
    if rl1~=n+1; error('Wrong number of rows in column label string'); end;
  end;
end;
SA=A;
A=fix(10^ndec*A+.5*sign(A))*10^(-ndec);
B=abs(fix(A));
mxlr=n*(4+length(int2str(max(max(B))))+ndec);
C=abs(round(10^ndec*(A-fix(A))));
for i=1:m;
  strr='';
  for j=1:n;
    sc=int2str(C(i,j));
    if length(sc)<ndec;
      for k=1:ndec-length(sc);
        sc=['0',sc];
      end;
    end;
    if SA(i,j)>=0; 
      strr=[strr,'&& ',int2str(B(i,j)),'.',sc];
    else;
      strr=[strr,'&&-',int2str(B(i,j)),'.',sc];
    end;
  end;
  if length(strr)<mxlr;
    for k=1:mxlr-length(strr);
      strr=[strr,' '];
    end;
  end;
  if i<m; 
    textab=[textab;[strr,'&\cr\tablerule   ']];
  else;
    textab=[textab;[strr,'&\cr\tablerule}}}']];
  end;
end;
if case>2;
  for i=1:rl1;
    l1str=[l1str;['&&\omit\hidewidth ',lab1(i,:),'\hidewidth']];
  end;
  tenspp=setstr(ones(1,cl1+10)*32);
  l1str=[l1str;['&\cr\widetablerule',tenspp]];
end;
if case==2 | case==4;
  for i=1:rl2;
    l2str=[l2str;['&&',lab2(i,:)]];
  end;
  textab=[l2str,textab];
end;
if case>2;
  [rt,ct]=size(l1str);
  [rb,cb]=size(textab);
  if cb>ct; textab=[l1str,setstr(ones(rt,cb-ct)*32);textab]; end;
  if cb<ct; textab=[l1str;textab,setstr(ones(rb,ct-cb)*32)]; end;
end;      
function textab=textab4(M,S,ndec,lab1,lab2);
%TEXTAB4 is an extension of TEXTAB2 and converts numerical tables
%  M and S in matlab to a table padded with the appropriate code for tex 
%  files TABTYP<i>.TEX located in subdirectory [MCGRATTAN.TEX]. 
%  This version of TEXTAB allows for labels in row 1, lab1, and labels 
%  in column 1, lab2.  In tex, you get a matrix of the form:
%
%   ------------------------------------------------------------------------
%   | lab1(1,:) || lab1(2,:) | lab1(3,:) | lab1(4,:) |  ...    |lab1(n+1,:)|
%   ________________________________________________________________________
%   ------------------------------------------------------------------------
%   | lab2(1,:) ||   M(1,1)  |   M(1,2)  |   M(1,3)  |  ...    |   M(1,n)  |
%   |           ||  (S(1,1)) |  (S(1,2)) |  (S(1,3)) |  ...    |  (S(1,n)) | 
%   ------------------------------------------------------------------------
%   | lab2(2,:) ||   M(2,1)  |   M(2,2)  |   A(2,3)  |  ...    |   M(2,n)  |
%   |           ||  (S(2,1)) |  (S(2,2)) |  (S(2,3)) |  ...    |  (S(2,n)) | 
%   ------------------------------------------------------------------------
%   |           :                             :                       |
%   ------------------------------------------------------------------------
%   | lab2(m,:) ||   M(m,1)  |   M(m,2)  |   M(m,3)  |  ...    |   M(m,n)  |
%   |           ||  (S(1,1)) |  (S(m,2)) |  (S(m,3)) |  ...    |  (S(m,n)) | 
%   ------------------------------------------------------------------------
%
%  
%  INPUTS: 
%    (i) M = mxn matrix 1 for table (e.g. a matrix of means)
%   (ii) S = mxn matrix 2 for table (e.g. a matrix of standard deviations)
%  (iii) ndec = number of decimal places to use for all values in table
%   (iv) lab1= a string matrix with n+1 rows if lab2~=0, n rows if lab2=0,
%              and an arbitrary number of columns.  If lab2~=0, row i of 
%              lab1 should contain a label for the (i-1)st column of A,
%              i=1,..n+1. If lab2=0, row i of lab1 should contain a label 
%              for the ith column of A, i=1,..n.
%              If lab1=0, column labels are not created.
%    (v) lab2= a string matrix with m rows and an arbitrary number of 
%              columns.  Row i of lab2 should contain a label for the ith 
%              row of A.
%              If lab2=0, row labels are not created.
% 
%  OUTPUT: textab = string to be read into one of the TABTYP<i>.TEX files
%
%  NOTES: See TEXTAB<j>.M, j=1,..4, for additional table choices
%
if max(max(imag(M)~=zeros(m,n)))>0 | max(max(imag(S)~=zeros(m,n)))>0;
  disp('Warning: complex number encountered.  Code needs updating for this')
end;
[m,n]=size(M);
if [m,n]~=size(S); error('The two matrices must have the same dimensions'); end;
[rl1,cl1]=size(lab1);
[rl2,cl2]=size(lab2);
case=1;                                       % neither row nor column labels
if lab1==0;                                   % row labels but no column labels
  if lab2~=0;
    case=2;
    if m~=rl2; error('Wrong number of rows in row label string'); end;
  end;
end;
if lab1~=0                                    % column labels but no row labels
  if lab2==0;
    case=3;
    if rl1~=n; error('Wrong number of rows in column label string'); end;
  end;
end;
if lab1~=0;                                   % both column and row labels
  if lab2~=0;
    case=4;
    if rl1~=n+1; error('Wrong number of rows in column label string'); end;
  end;
end;
M=fix(10^ndec*M+.5*sign(M))*10^(-ndec);
S=fix(10^ndec*S+.5*sign(S))*10^(-ndec);
SM=M;
SS=S;
A=abs(fix(M)); 
B=abs(fix(S));
mxlr=n*(6+length(int2str(max(max(max(A,B)))))+ndec);   
C=abs(round(10^ndec*(M-fix(M))));
D=abs(round(10^ndec*(S-fix(S))));
for i=1:m;
  strr1='';
  strr2='';
  for j=1:n;
    sc=int2str(C(i,j));
    if length(sc)<ndec;
      for k=1:ndec-length(sc);
        sc=['0',sc];
      end;
    end;
    sd=int2str(D(i,j));
    if length(sd)<ndec;
      for k=1:ndec-length(sd);
        sd=['0',sd];
      end;
    end;
    if SM(i,j)>=0; 
      strr1=[strr1,'&&  ',int2str(A(i,j)),'.',sc,' '];
    else;
      strr1=[strr1,'&& -',int2str(A(i,j)),'.',sc,' '];
    end;
    if SS(i,j)>=0; 
      strr2=[strr2,'&&( ',int2str(B(i,j)),'.',sd,')'];
    else;
      strr2=[strr2,'&&(-',int2str(B(i,j)),'.',sd,')'];
    end;
  end;
  if length(strr1)<mxlr;
    for k=1:mxlr-length(strr1);
      strr1=[strr1,' '];
    end;
  end;
  if length(strr2)<mxlr;
    for k=1:mxlr-length(strr2);
      strr2=[strr2,' '];
    end;
  end;
  if i<m; 
    textab=[textab;[strr1,'&\cr             '];[strr2,'&\cr\tablerule   ']];
  else;
    textab=[textab;[strr1,'&\cr             '];[strr2,'&\cr\tablerule}}}']];
  end;
end;
if case>2;
  for i=1:rl1;
    l1str=[l1str;['&&\omit\hidewidth ',lab1(i,:),'\hidewidth']];
  end;
  tenspp=setstr(ones(1,cl1+10)*32);
  l1str=[l1str;['&\cr\widetablerule',tenspp]];
end;
if case==2 | case==4;
  for i=1:rl2;
    l2str=[l2str;'&&',lab2(i,:);'&&',setstr(ones(1,cl2)*32)];
  end;
  textab=[l2str,textab];
end;
if case>2;
  [rt,ct]=size(l1str);
  [rb,cb]=size(textab);
  if cb>ct; textab=[l1str,setstr(ones(rt,cb-ct)*32);textab]; end;
  if cb<ct; textab=[l1str;textab,setstr(ones(rb,ct-cb)*32)]; end;
end;      
function textab=texmat(A,ndec);
%TEXMAT converts a numerical matrix A in matlab to a matrix for tex.
%  
%  INPUTS: 
%    (i) A = matrix for table
%   (ii) ndec = number of decimal places to use for all values
% 
%  OUTPUT: textab = string to be read into a *.TEX file
%
[m,n]=size(A);
if max(max(imag(A)~=zeros(m,n)));
  disp('Warning: complex number encountered. Code needs updating for this.');
end;
SA=A;
A=fix(10^ndec*A+.5*sign(A))*10^(-ndec);
B=abs(fix(A));
mxlr=n*(4+length(int2str(max(max(B))))+ndec);
C=abs(round(10^ndec*(A-fix(A))));
for i=1:m;
  strr='';
  for j=1:n;
    sc=int2str(C(i,j));
    if length(sc)<ndec;
      for k=1:ndec-length(sc);
        sc=['0',sc];
      end;
    end;
    if SA(i,j)>=0; 
      strr=[strr,' ',int2str(B(i,j)),'.',sc,'&'];
    else;
      strr=[strr,'-',int2str(B(i,j)),'.',sc,'&'];
    end;
  end;
  strr=strr(1:length(strr)-1);
  if length(strr)<mxlr;
    for k=1:mxlr-length(strr);
      strr=[strr,' '];
    end;
  end;
  if i<m; 
    textab=[textab;[strr,'\cr        ']];
  else;
    textab=[textab;[strr,'\cr}\right]']];
  end;
end;
textab=[['\left[\matrix{';setstr(ones(m-1,14)*32)],textab];
