function flist = subdir(rootdir)
% flist = subdir(rootdir) gather filenames from subfolders
%
% rootdir [string] directory name
% flist [structure] list of files with fields  .dir, .name, .date, .bytes
%
% Example
% flist = subdir('d:/mathnb')
%
% http://strijov.com

if ~exist(rootdir,'dir'), error('no such directory'); end

flist = []; % initial list of the gathered filenames
flist = gatherflist(rootdir,flist);
%save flist_file flist
return

function flist = gatherflist(dirname,flist)
% recursive gathering of the file list
 d=dir(dirname);      
 %search for the very first file in the list
 istart = 3;
 for i = 1:length(d);     
     if strcmp(d(i).name,'..')
         istart = i+1;
         break
     end
 end
    
 for i = istart:length(d);
     if d(i).isdir
         newdir = [dirname,'\',d(i).name];
         fprintf(1,'%s\n',newdir);
         flist = gatherflist(newdir, flist);
     else
         flist(end+1).dir  = dirname;
         flist(end).name = d(i).name;
         flist(end).date = d(i).date;
         flist(end).bytes = d(i).bytes;
     end
 end
 return