function mecg2hr(datapath, process)
if (nargin == 0) datapath = pwd; end %if
if (nargin < 2) process = 'unprocessed'; end %if
if ~exist(datapath, 'dir') disp('Mecg2hr: Data path is not a valid directory'); return; end %if
datafiles = [name(dir([datapath filesep '*.TXT'])); name(dir([datapath filesep '*.txt']))];
if (~strcmp(process, 'all')) datafiles = onlynew(datapath, datafiles); end %if
i = 0;
while (i < length(datafiles))
  i = i + 1;
  disp(sprintf('Mecg2hr is currently processing file: %s', char(datafiles(i))));
  chooser('choosenew', char(datafiles(i)), datapath);
  try
    ecg2hr;
  catch
    if strcmp(lasterr, 'Data from ECG input file is not valid.  Please run Splice if appropriate.')
      [newdatafiles] = splice;
      datafiles = [datafiles; newdatafiles];
    end %if
  end %try
end %while
if (i == 0) disp('Mecg2hr: All data files have already been processed'); end %if
disp(sprintf('Mecg2hr: %d files processed', i));

function [datafiles] = name(datastruct)
datafiles = {};
for i = 1:size(datastruct, 1);
  datafiles = [datafiles; {datastruct(i).name}];
end %for

function [specificfiles] = onlynew(datapath, datacells)
specificfiles = {};
for i = 1:size(datacells, 1);
  [null ecgtitle] = fileparts(char(datacells(i)));
  etee = length(ecgtitle);
  eteb = max(1, etee-3);
  cdfilename = [datapath filesep ecgtitle '.cdd'];
  if (~exist(cdfilename, 'file') & ~strcmp(ecgtitle(eteb:etee), 'info') & ~strcmp(ecgtitle(eteb:etee), 'time'))
    specificfiles = [specificfiles; datacells(i)];
  end %if
end %for
