function sorted_struct = dirs(str_descr, varargin)
% sorted_struct = dirs(str_descr, varargin)
% return an ascii-sorted directory listing 
% str_descr is whatever string descriptor one would normally use with "dir"
%
% varargin is name, value pairs: if not set, act as the original
% matlab "dir" command does
%
% varargin:
% name        values               comments
%
% 'sort'     'none'                matlab's original order (why???)
%            'ascii'               default
% 'fullpath',  [0 or 1]           0 (do not include full path in name)
%
%
% when someone gets ambitious they can add sorting by date
%
%

%JH 2001/04/06

cfg.sort = [];
cfg.fullpath = 0;


cfg = fillstruct(cfg, varargin, 0);


dstruct = dir(str_descr);

if (nargin == 1) | (strmatch(cfg.sort, 'ascii'))
   %sort dirlist by ascii
   fnames = fieldnames(dstruct);
   dcell = struct2cell(dstruct);   %(4 X nfiles)
   [sorted_names, sorti] = sort(dcell(1,:));
   sorted_dcell = dcell(:,sorti);
   sorted_struct = cell2struct(sorted_dcell', fnames(:)',2);
elseif (strmatch(cfg.sort, 'none'))
   sorted_struct = dstruct;
else 
   help(mfilename)
   error('can only sort by ''ascii'' or ''none'' at the moment')
end


if (cfg.fullpath)
   if (isdir(str_descr))
      dirstr = str_descr;
   else %assume it was a wildcard description
      [pp,nn,ee]=fileparts(str_descr);
      dirstr = pp;
   end
   
   for ii=1:length(sorted_struct)
      sorted_struct(ii).name = fullfile(dirstr, sorted_struct(ii).name);
   end
end



   







