function COUNT = writelabel(LABELS, FILES)
%
% COUNT = writelabel(LABELS, FILES)
% 
% Write the NIST label file specified in FILE
%
% FILES (string) - the name of the file to be read
% LABELS (1x3) - a cell array containing the times and labels:
%      LABELS{1,1} - a vector of start times
%      LABELS{1,2} - a vector of end times
%      LABELS{1,3} - a cell array containing the label texts
%
% If FILES is a cell array containing several filenames, 
% and if LABELS has the same number of rows, then the rows of LABELS will
% be written to the filenames given in FILENAMES.
%
% Mark Hasegawa-Johnson
% Last revised 2/18/2002

% Initialize COUNT
COUNT=0;

% Check to see if FILES is a cell array
if ~iscell(FILES),
   FILES={ FILES };
end

% Number of files to write = minimum of length(FILES), number of rows in LABELS
NF = min(length(FILES), size(LABELS,1));

% Write the files
for ifile=1:NF,
   fid=fopen(FILES{ifile},'w');
   if fid>0,
      % Write out all rows
      for r=1:length(LABELS{ifile,1}),
         COUNT = COUNT + fprintf(fid,'%g %g %s\n',...
            LABELS{ifile,1}(r),LABELS{ifile,2}(r),LABELS{ifile,3}{r});
      end
      fclose(fid);
   else
      disp(sprintf('writelabel: Unable to open file %s for writing',FILES{ifile}));
   end
end

