% PURPOSE  : 	Clears ERPset(s)
% 
% FORMAT   : 
% 
% >> ALLERP = pop_deleterpset( ALLERP, index);  
% 
% EXAMPLE  :
% 
% >> ALLERP = pop_deleterpset( ALLERP, [3]);   
% 
% INPUTS   :
% 
% ALLERP    - Includes all ERPsets in workspace
% Index     - ERPset(s) that you want to clear from the workspace  
% 
% OUTPUTS  :
% 
% - updated (output) ALLERP. Will include all ERPsets, minus that deleted 
%
% *** This function is part of ERPLAB Toolbox ***
% Author: Javier Lopez-Calderon
% Center for Mind and Brain
% University of California, Davis,
% Davis, CA
% 2009

function [ALLERP erpcom] = pop_deleterpset(ALLERP, index)
erpcom = '';

if nargin<1
      help pop_deleterpset
end
if nargin<2      
      try
            CURRENTERP = evalin('base', 'CURRENTERP');
      catch
            CURRENTERP = 0;
      end      
      if  CURRENTERP == 0
            msgboxText{1} =  'ERPsets menu is already empty...';
            title        =  'ERPLAB: no erpset(s)';
            errorfound(msgboxText, title);
            return
      end      
      prompt    = {'Erpset(s) to clear:'};
      dlg_title = 'Delete erpset(s)';
      num_lines = 1;      
      def = {num2str(CURRENTERP)}; %01-13-2009
      answer = inputvalue(prompt,dlg_title,num_lines,def);
      
      if isempty(answer)
            disp('User selected Cancel')
            return
      end
      
      index =  str2num(answer{1});      
elseif nargin>2
      disp('wrong inputs')
      return
end

nerpset = length(ALLERP);
index   = unique(index);

if isempty(index)
      msgboxText{1} =  'Wrong erpset index(es)';
      title        =  'ERPLAB: unrecognizable erpset(s)';
      errorfound(msgboxText, title);
      return
end
if max(index)>nerpset || min(index)<1     
      erpm     = findobj('tag', 'linerp');
      nerpmenu = length(erpm);
      if max(index)<=nerpmenu && nerpmenu>=1 && max(index)>=1
            
      else
            msgboxText = ['Wrong erpset index(es)\n'...
                  'Check your erpset menu or write length(ALLERP) at command window for comprobation'];
            title        =  'ERPLAB: pop_deleterpset not existing erpset(s)';
            errorfound(sprintf(msgboxText), title);
            return
      end
end
detect = ~ismember(1:nerpset,index);
newindex = find(detect);
if isempty(newindex)
      ALLERP = [];
else
      ALLERP = ALLERP(newindex);
end
updatemenuerp(ALLERP, -1)
erpcom = sprintf('ALLERP = pop_deleterpset( ALLERP, [%s]);', num2str(index));

%
% Completion statement
%
msg2end
return
