function varargout = statstr(varargin)
% ------------------------------------------------------------------
% Display or log current statistics.
%
% SYNOPSIS:
%
%
%   statstr(options)
%   statstr(stat, options)
%   s = statstr(options)
%   s = statstr(stat, options)
%
% PARAMMETERS:
%
%   stat
%      Character specifying which statistics to display. If absent, all
%      available statistics are displayed.
%
%   options
%      A list of header strings to be displayed on top of the statistics.
%      Array containing the variable numbers.
%
% Possible values for the statistics character are described in function
% `limp'.
%
% When called with a return argument, then limit string is not displayed
% but only returned.
% ------------------------------------------------------------------
%Time:<Last updated on 98/09/16 at 15:37:55 by jaccard@pc34.gfi.uib.no>
%File:</home1/jaccard/data1/codas3/gfi/matlab/matlab50/statstr.m>

scanglob;

stats = '';
if nargin == 0,
  stats = 'SP';
  l0 = 1; 
end
if (nargin > 0) & (length(varargin{1}) == 1),
  stats = varargin{1};
  l0 = 2;
end

% Get the headers:
s = '';
for i=l0:nargin,
  s = [s varargin{i}];
end


% Loop through the statistics:
for i=1:length(stats),
  
  stat = stats(i);
  
  switch stat
	 case 'S',
		st   = SSTAT;
		nums = VARNUMS;
		names= VARNAMES;
		dmin = SMIN;
		dmax = SMAX;
		dlim = SLIM;
		dind = SIND;
	 case 'P',
		st   = PSTAT;
		nums = PVARS;
		names= VARNAMES(PVARS, :);
		dmin = PMIN;
		dmax = PMAX;
		dlim = PLIM;
		dind = PIND;
	 otherwise,
		error(['Unknown statistics ' stat]);
  end

  n = size(st,1);
  
  % Print the header:
  % 98/07/16 PJ Commented lines below, because header information is
  %             inserted in `limp'.
  %shdr = stathdr(stat);
  %s = sprintf('%s%s', s, shdr);
  
  % Print limits:
  slim = limp(stat);
  s = sprintf('%s%s', s, slim);
  
  % Padding for variable names:
  l = size(names,2) - length('Name') - 1;
  pad = '';
  for i=1:l
	 pad = [pad '.'];
  end

  % Print table header:
  s = sprintf('%s\n %s%s', s, 'COL...  NAME', pad);
  s = sprintf('%s %s', s, 'MIN......... MAX......... IMIN.. IMAX..');
  s = sprintf('%s %s\n', s, 'MEAN........ SDEV........ NPTS..');

  % Print statistics:
  for j=1:n,
	 s = sprintf('%s %6d %s %12.5f %12.5f %6d %6d %12.5f %12.5f %6d\n', s, ...
		  nums(j), names(j,:), ...
		  st(j,1), st(j,2), st(j,3), st(j,4), st(j,5), st(j,6), st(j,7));
  end
  s = sprintf('%s\n\n', s);

end

if nargout == 0,
  fprintf('%s', s);
else
  varargout{1} = s;
  % Print information in output file:
  if ASC,
	 fpout = fopen(ASCFILE, 'a');
	 fprintf(fpout, '\n%s\n\n', ...
		  '----------------------------------------------------------------', s);
	 fclose(fpout);
  end
end
  
return;


