function str=table(M,S,s,rlabel,clabel1,clabel2);
%TLABMSD creates a table of the form:
%
%    -------------------------------------------------------------
%    |       |                      clabel1                      |
%    |       |----------------------------------------------------
%    | rows  |  clabel21  |  clabel22  |  clabel23  |  clabel24  | ...
%    -------------------------------------------------------------
%    |rlabel1|   M(1,1)   |   M(1,2)   |   M(1,3)   |   M(1,4)   | 
%    |       |  (S(1,1))  |  (S(1,2))  |  (S(1,3))  |  (S(1,4))  |
%    ------------------------------------------------------------- ...
%    |rlabel2|   M(2,1)   |   M(2,2)   |   M(2,3)   |   M(2,4)   | 
%    |       |  (S(2,1))  |  (S(2,2))  |  (S(2,3))  |  (S(2,4))  |
%    -------------------------------------------------------------
%    |rlabel3|   M(3,1)   |   M(3,2)   |   M(3,3)   |   M(3,4)   | ...
%    |       |  (S(3,1))  |  (S(3,2))  |  (S(3,3))  |  (S(3,4))  |
%    -------------------------------------------------------------
%          :                    :                     :
%
%  INPUTS: 
%    (i) M = mxn matrix for table  (e.g. means of estimates)
%   (ii) S = mxn matrix for elements in ( ) in table (e.g. standard errors)
%  (iii) s = number of spaces padding the number in the table (which has
%            converted to a 10-space string)
%   (iv) rlabel= (m+1) x max(row label) string matrix with labels for rows.
%            For example, rlabel=[' rows  ';'rlabel1';'rlabel2';'rlabel3']
%    (v) clabel1= string row vector with the first labels for the columns
%   (vi) clabel2= n x max(2nd col. labels) string matrix with labels for
%            columns.  For example, clabel2='clabel21';'clabel22'; ..
%            'clabel23';'clabel24']
%
%  OUTPUTS: str= a string matrix giving the table of the form above.
%
[m,n]=size(M);
[rr,rc]=size(rlabel);
[cr1,cc1]=size(clabel1);
[cr2,cc2]=size(clabel2);
if m~=rr-1; 
  error('no. of rows in matrix ~= no. of rows in row-labels matrix minus 1');
end;
if n~=cr2; 
  error('no. of columns in matrix ~= no. of rows in column-labels matrix ');
end;
p=2*s+12;
if cc2>2*s+12; 
  error('Make your column titles shorter or the space between columns longer.')
end;
for i=1:s; pad1(1,i)=' '; end;
for i=1:n*(p+1); bars(1,i)='-'; end;
for i=1:m;
  for j=1:n;
    snum1=num2str(M(i,j));
    snum2=num2str(S(i,j));
    for k=1:s+11-length(snum1); pad21(1,k)=' '; end;
    if length(snum2)<10;
      for k=1:s+10-length(snum2); pad22(1,k)=' '; end;
    end;
    str(3*i-2,(j-1)*(p+1)+1:j*(p+1))=[pad21,snum1,pad1,' ','|'];
    str(3*i-1,(j-1)*(p+1)+1:j*(p+1))=[pad22,'(',snum2,')',pad1,'|'];
    clear pad21
    clear pad22
  end;
  str(3*i,:)=bars;
end;
clear bars
for i=1:rc;
  pad3(1,i)=' ';
end;
for i=1:rc+2;
  bars(1,i)='-';
end;
for i=1:rr;
  rstr(3*i-2,:)=['|',rlabel(i,:),'|'];
  rstr(3*i-1,:)=['|',pad3,'|']; 
  rstr(3*i,:)=bars;
end;
rstr=[rstr(1,:);rstr(3:3*rr,:)];
rstr=['|',pad3,'|';'|',pad3,'|';rstr];
if ((rem(n,2)==0 & rem(cc1,2)==0) | (rem(n,2)==1 & rem(cc1,2)==1));
  tl=(n*(p+1)-2-cc1)/2;
  title1=[clabel1,' '];
else;
  tl=(n*(p+1)-1-cc1)/2;
  title1=clabel1;
end;
for i=1:tl;
  pad4(1,i)=' ';
end;
title1=[pad4,title1,pad4,'|'];
for i=1:cr2;
  pad5(i,1)=' ';
end;
if rem(cc2,2)==1; clabel2=[clabel2,pad5]; cc2=cc2+1; end;
for i=1:(p-cc2)/2;
  pad6(1,i)=' ';
end;
for i=1:n;
  title2(1,(i-1)*(p+1)+1:i*(p+1))=[pad6,clabel2(i,:),pad6,'|'];
end;
str=[rstr(4,:) str(3,:);rstr [title1;str(3,:);title2;str(3,:);str]];
