function [basefile_out,datafile_out,failed] = match_epochs (basefile_in,datafile_in,process)
% match_epochs: match two matrices by values in the first columns, discard all non-matched values
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% find out if any epochs overlap, if none then return empty sets;

if (process==1)
   % initialize output
   basefile_out=[];
   datafile_out=[];
   
   % verify that there are overlapping epochs
   [int_set,base_idx,data_idx]=intersect(basefile_in(:,1),datafile_in(:,1));
   if (isempty(int_set)==1)
      err_text='There are no matching times in "match epochs"!';
      fault(err_text);
      return;
   end;
   
   % build new vectors of common times
   basefile_out=basefile_in(base_idx,:);
   datafile_out=datafile_in(data_idx,:);
elseif (process==2)
   % verify that there are overlapping epochs
   [u_set,j01,data_idx]=union(basefile_in(:,1),datafile_in(:,1));
   [j02,j03,base_idx]=union(datafile_in(:,1),basefile_in(:,1));
   if (isempty(u_set)==1)
      err_text='There are no matching times in "match epochs"!';
      fault(err_text);
      return;
   end;
   
   % initialize output
   basefile_out=nan*ones(size(u_set,1),size(basefile_in,2));
   datafile_out=nan*ones(size(u_set,1),size(datafile_in,2));
   
   % build new vectors of common times
   basefile_out(:,1)=u_set;
   basefile_out(base_idx,:)=basefile_in;
   datafile_out(:,1)=u_set;
   datafile_out(data_idx,:)=datafile_in;
else
   err_text='Unknown processing type in "match epochs"!';
   fault(err_text);
   return;
end;


% % find common beginning times
% while (basefile(1,1)~=datafile(1,1))
%    min_time=min(intersect(basefile(:,1),datafile(:,1)));
%    start1=find(basefile(:,1)==min_time);       % idx of start times
%    start2=find(datafile(:,1)==min_time);
%    if start1>=1
%       basefile(1:start1-1,:)=[];
%    end;
%    if start2>=1
%       datafile(1:start2-1,:)=[];
%    end;
% end;
% 
% % find common end times
% while (basefile(end,1)~=datafile(end,1))
%    max_time=max(intersect(basefile(:,1),datafile(:,1)));
%    end1=find(basefile(:,1)==max_time);       % idx of end times
%    end2=find(datafile(:,1)==max_time);
%    if end1<=size(basefile,1)
%       basefile(end1+1:end,:)=[];
%    end;
%    if end2<=size(datafile,1)
%       datafile(end2+1:end,:)=[];
%    end;
% end;
% 
% %fprintf('removed beginning & end lines to match files\n');
% min_size=min([size(basefile,1) size(datafile,1)]);
% remainder=basefile(1:min_size,1)-datafile(1:min_size,1);
% j01=find(remainder~=0);
% while ((size(basefile,1)~=size(datafile,1))|(isempty(j01)==0)) %(basefile(end,1)~=max_time)|(datafile(end,1)~=max_time)|
%    j01=find(remainder~=0);
%    if (isempty(j01)==0)
%       if (abs(remainder(j01(1,1),1))==1)
%          if (remainder(j01(1,1),1)<0)
%             basefile(j01(1,1),:)=[];
%          elseif (remainder(j01(1,1),1)>0)
%             datafile(j01(1,1),:)=[];
%          end;
%       elseif (abs(remainder(j01(1,1),1))>1)
%          if (remainder(j01(1,1),1)<0)
%             j02=find(basefile(:,1)>=datafile(j01(1,1),1));
%             if (size(j02,1)>0)
%                basefile(j01(1,1):(j02(1,1)-1),:)=[];
%             else
%                err_text='"match epochs" big problem: 01';
%                fault(err_text);
%                return;
%             end;
%          elseif (remainder(j01(1,1),1)>0)
%             j02=find(datafile(:,1)>=basefile(j01(1,1),1));
%             if (size(j02,1)>0)
%                datafile(j01(1,1):(j02(1,1)-1),:)=[];
%             else
%                err_text='"match epochs" big problem: 02';
%                fault(err_text);
%                return;
%             end;
%          end;
%       else
%          err_text='"match epochs" big problem: 03';
%          fault(err_text);
%          return;
%       end;
%    end;
%    clear min_size j01 j02 remainder;
%    min_size=min([size(basefile,1) size(datafile,1)]);
%    remainder=basefile(1:min_size,1)-datafile(1:min_size,1);
%    j01=find(remainder~=0);
% end;
% if (size(basefile,1)~=size(datafile,1))
%    err_text='Array sizes are different!';
%    fault(err_text);
%    return;
% end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
failed=0;
return;
