function  save_sbj_database(filename,DB,mode)

%SAVE_SBJ_DATABASE -- Writes 'subject descriptor' structures to file.
%
%  save_sbj_database(filename,DB)
%  save_sbj_database(filename,DB,'-ascii')
%
%  FILENAME is a string (without the '.mat' suffix).
%  DB is a structure array of 'subject descriptors' (see NEW_SBJ_DESCR).
%  (Note: Unlike the SAVE command, DB is not a string but the var. itself.)
%  When the third argument, MODE, is omitted or is set to '-mat' (the
%  default), the structure is simply written using MATLAB's built-in
%  function SAVE. The filename suffix is '.mat', e.g. 'mysubjects.mat'.
%  When MODE='-ascii' or '-ASCII', 	SAVE_SBJ_DATABASE writes an ASCII
%  file capable of being executed as a script. The filename suffix in
%  this case is '.m', e.g. 'mysubjects.m'.
%
%  See also SAVE, UPDATE_SBJ_DATABASE, OPEN_SBJ_DATABASE.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.2 $  $Date: 2004/03/13 17:50 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

if (nargin<3)
   save(filename,'DB') ;        % -MAT mode
elseif  (strcmpi(mode,'-MAT'))
   save(filename,'DB') ;
elseif  (~strcmpi(mode,'-ASCII'))
   error('MODE should be either ''-MAT'' (the default) or ''-ASCII''.') ;
else                            % -ASCII mode
   %-- Create an ASCII file
   filename = [filename,'.m'] ;
   [fid,errmsg] = fopen(filename,'wt') ;
   if (fid == -1)  error(errmsg) ; end
   fprintf(fid,'%% This file has been generated by SAVE_SBJ_DATABASE.\n') ;
   fprintf(fid,'%% Execute it as a script to create a variable DB.\n');
   %-- Write the descriptors, one by one
   for k = 1:length(DB)
      fprintf(fid,'\nk = %d ;\n',k) ;
      fprintf(fid,'DB(k).sbj_number = %d ;\n',DB(k).sbj_number) ;
      fprintf(fid,'DB(k).group = %d ;\n',DB(k).group) ;
      fprintf(fid,'DB(k).name = ''%s'' ;\n',DB(k).name) ;
      fprintf(fid,'DB(k).comment = ''%s'' ;\n',DB(k).comment) ;
      fprintf(fid,'DB(k).filename = ''%s'' ;\n',DB(k).filename) ;
      fprintf(fid,'DB(k).session = %d ;\n',DB(k).session) ;
      sl = DB(k).session_log ;
      if (length(sl)==0)
         empty_sl = 'struct(''session'',{},''date'',{},''trials'',{})' ;
         fprintf(fid,'DB(k).session_log = %s ;\n',empty_sl) ;
      else
         for s = 1:length(sl)
            fprintf(fid,'DB(k).session_log(%d) = struct(',s) ;
            fprintf(fid,'''session'',%d,',sl(s).session) ;
            fprintf(fid,'''date'',''%s'',',sl(s).date) ;
            fprintf(fid,'''trials'',%d) ;\n',sl(s).trials) ;
         end % for s
      end % if length(sl)
      %- Write the expmt field, if any
      if (isfield(DB(k),'expmt'))
         fprintf(fid,'DB(k).expmt = %d ;\n',DB(k).expmt) ;
      end % if NCache_sequence
      %- Write the NCache_sequence field, if any
      if (isfield(DB(k),'NCache_sequence'))
         s = vect2str(DB(k).NCache_sequence) ;
         fprintf(fid,'DB(k).NCache_sequence = %s ;\n',s) ;
      end % if NCache_sequence
      %- Write the stair_levels field, if any
      if (isfield(DB(k),'stair_levels'))
         s = mat2str(DB(k).stair_levels) ;
         fprintf(fid,'DB(k).stair_levels = %s ;\n',s) ;
      end % if NCache_sequence
   end % for k
   %-- Close up
   fprintf(fid,'\n%% End of file %s.  %s.\n',upper(filename),datestr(now));
   fclose(fid) ;
end

%--- Return no value
%%%%% End of file SAVE_SBJ_DATABASE.M
