function batmergeavgfiles(filename,filepath,mode)
%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      



if mode==0
   modefehler
   return

% directory mode   
elseif mode==11
	cd (filepath);
	filelist=dir(filepath);
   filelistsize=size(filelist);
   [outfile,outpath]= uiputfile('*.*','Please create dummy-file in target directory:');
   
   prompt={'Enter the weighting-mode: [1] none [2] trial [3] std-matrix:','Enter the normalization-mode:  [1] none [2] RMS [3] GP:','Print global power for each file? [y]/[n]'};
   def={'2','1','n'};
   dlgTitle='Please enter average-preferences:';
   lineNo=1;
   answer=inputdlg(prompt,dlgTitle,lineNo,def);
   
	% Schleife für Files
   for filezaehler=3:filelistsize(1)
      [filemat,mode]=batchfiletomat(filelist(filezaehler).name,[],0);
      mergeavgfilespp(char(filemat),[],str2num(answer{1}),str2num(answer{2}),[],outpath);
   end
   ok
   
% batchfile mode   
elseif mode==12
   [outfile,outpath]= uiputfile('*.*','Please create dummy-file in target directory:');
   
   prompt={'Enter the weighting-mode: [1] none [2] trial [3] std-matrix:','Enter the normalization-mode:  [1] none [2] RMS [3] GP:','Print global power for each file? [y]/[n]'};
   def={'2','1','n'};
   dlgTitle='Please enter average-preferences:';
   lineNo=1;
   answer=inputdlg(prompt,dlgTitle,lineNo,def);
   
   batfilemat=batchfiletomat(filename,filepath);
   filematgroessevec=size(batfilemat);
   filematgroesse=filematgroessevec(2);
   for l=1:filematgroesse
      [filemat,mode]=batchfiletomat(batfilemat{l},[],0);
      mergeavgfilespp(char(filemat),batfilemat{l},str2num(answer{1}),str2num(answer{2}),[],outpath);
   end
   return
      
% single file mode   
elseif mode==2
  
      [filemat,mode]=batchfiletomat(filename,[],0);
      mergeavgfilespp(char(filemat),[],str2num(answer{1}),str2num(answer{2}),[],outpath);
   ok
  
end
return