function NHA65mergedata()
%
% Merges data files generated by individual runs of NHA65CW.m
%
% Assumes this function is being run in the directory that has the data
% files. The data files must all have .txt extensions, and no other files
% in the directory can have .txt extensions!
%
% John Kruschke, 09-Feb-2003, 28-Feb-2005.

% Let it be known that this program is running:
fprintf(1,'Running NHA65mergedata...\n');

% Get a list of all *.txt files, which should be all and only the data
% files.
filelist = dir('*.txt');
numfiles = size(filelist,1);
if numfiles == 0
   disp('** NO DATA FILES FOUND. **')
   return;
end

% Initialize master vectors for accumulating data.
Mid = [];
Mblock  = [];
MtrialInBlock = [];
MtrialOverall  = [];
Mtype  = [];
Mtoken  = [];
Mrating  = [];
Mrt = [];

% Loop through datafiles, appending info to master vectors. This depends on
% the specific structure of data generated by NHA65CW.m
for fileindex = 1 : numfiles

   % Read data from the individual file.
   [id block trialInBlock trialOverall type token rating rt] = ...
      textread(filelist(fileindex).name,'%s %d %d %d %s %s %d %f');

   % Check length of data vectors. Should be 80; i.e., each subject's
   % file should be 2 blocks of 40 trials. If not, complain and exit.
   if length(id) ~= 80
      error(['Data file ' filelist(fileindex).name ...
         ' does not have 80 trials. Repair and try again.']);
   end

   % Append data to the master vectors.
   Mid = [ Mid ; id ];
   Mblock = [ Mblock ; block ];
   MtrialInBlock = [ MtrialInBlock ; trialInBlock ];
   MtrialOverall = [ MtrialOverall ; trialOverall ];
   Mtype = [ Mtype ; type ];
   Mtoken = [ Mtoken ; token ];
   Mrating = [ Mrating ; rating ];
   Mrt = [ Mrt ; rt ];

end

% Open master file and store master vectors. The master file is not given a
% '.txt' extension because this would be included in the filelist if the
% program were run a second time, and then the current master file would be
% re-merged into the next master file.
masterFilename = 'NHA65merge.dat';

% Check whether that master file already exists.
previouslyMerged = dir(masterFilename);
if size(previouslyMerged,1) > 0
   disp('** PREVIOUSLY EXISTING MERGE FILE WILL BE DELETED **')
   disp('Press Enter to continue; cntrl-C to abort.')
   pause;
   delete(masterFilename);
end

% Write merged data to master file. The formatting is an arbitrary choice.
FileID = fopen(masterFilename,'w');
for rowIndex = 1 : size(Mid,1)
   fprintf(FileID,'%s',char(Mid(rowIndex)));
   fprintf(FileID,'\t');
   fprintf(FileID,'%4.0f',Mblock(rowIndex));
   fprintf(FileID,'\t');
   fprintf(FileID,'%4.0f',MtrialInBlock(rowIndex));
   fprintf(FileID,'\t');
   fprintf(FileID,'%4.0f',MtrialOverall(rowIndex));
   fprintf(FileID,'\t');
   fprintf(FileID,'%s',char(Mtype(rowIndex)));
   fprintf(FileID,'\t');
   fprintf(FileID,'%s',char(Mtoken(rowIndex)));
   fprintf(FileID,'\t');
   fprintf(FileID,'%4.0f',Mrating(rowIndex));
   fprintf(FileID,'\t');
   fprintf(FileID,'%7.3f',Mrt(rowIndex));
   fprintf(FileID,'\n');
end
fclose(FileID);

% ------------------------------------------------------------------------
