% Author: Javier Lopez-Calderon & Steven Luck
% Center for Mind and Brain
% University of California, Davis,
% Davis, CA
% 2007-2010

%b8d3721ed219e65100184c6b95db209bb8d3721ed219e65100184c6b95db209b
%
% ERPLAB Toolbox
% Copyright � 2007 The Regents of the University of California
% Created by Javier Lopez-Calderon and Steven Luck
% Center for Mind and Brain, University of California, Davis,
% javlopez@ucdavis.edu, sjluck@ucdavis.edu
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

function currvers = eegplugin_erplab(fig, trystrs, catchstrs)

erplabver = '3.0.2.1';      % current erplab version
erplabrel = '17-Oct-2012';  % DOB
currvers  = ['erplab_' erplabver];

if nargin < 3
        error('eegplugin_erplab requires 3 arguments');
end

%
% ADD FOLDER TO PATH
%
p = which('eegplugin_erplab', '-all');
if length(p)>1
        fprintf('\nERPLAB WARNING: More than one ERPLAB folder was found.\n\n');
end
p = p{1};
p = p(1:findstr(p,'eegplugin_erplab.m')-1);
if ~exist('pop_binlister.m','file')
        addpath(p);
        addpath([p 'erplab_help'], [p 'erplab_Temp'], [p 'functions'], [p 'GUIs'], [p 'images'], [p 'pop_functions']); % Thanks to Grega Repovs
end

%
% CHECK VERSION NUMBER & FOLDER NAME
%
foldernum = regexp(p,'erplab_(\d+\.+\d+\.+\d+\.+\d+)', 'tokens', 'ignorecase');
if ~strcmp(foldernum{:}, erplabver)
        fprintf('\nERPLAB WARNING: ERPLAB''s folder name does not match with the current version number.\n\n')
end

%
% TEMPORARY ERPLAB's Files 
%
dirTemp   = fullfile(p,'erplab_Temp');
filst     = dir(dirTemp);
filenames = {filst.name};

if length(filenames)>2
        recycle on;
        delete(fullfile(dirTemp,'*'))
        fprintf('\nERPLAB WARNING: Temporary files (from your last session) within erplab_Temp folder were sent to recycle bin.\n\n')
end

%
% ERPLAB's WORKING MEMORY
%
if exist('memoryerp.erpm','file')==2
         ColorB = erpworkingmemory('ColorB');
         ColorF = erpworkingmemory('ColorF');
         fontsizeGUI  = erpworkingmemory('fontsizeGUI');
         fontunitsGUI = erpworkingmemory('fontunitsGUI');
else
         %ColorB = [0.552941176470588   0.615686274509804   0.741176470588235];
         ColorB = [170 180 195]/255;
         ColorF = [0 0 0];
         fontsizeGUI  = 8.8;
         if ispc
                  fontunitsGUI = 'pixels';
         else
                  fontunitsGUI = 'points';
         end
end
mshock = 0;
save(fullfile(p,'memoryerp.erpm'), 'erplabrel','erplabver', 'ColorB', 'ColorF', 'fontsizeGUI', 'fontunitsGUI', 'mshock'); % saves erplab version

%
% ERPLAB's VARIABLES TO WORKSPACE
%
ERP              = [];  % Start ERP Structure on workspace
ALLERP           = [];  % Start ALLERP Structure on workspace
ALLERPCOM        = [];
CURRENTERP       = 0;
plotset.ptime    = [];
plotset.pscalp   = [];

assignin('base','ERP',ERP);
assignin('base','ALLERP', ALLERP);
assignin('base','ALLERPCOM', ALLERPCOM);
assignin('base','CURRENTERP', CURRENTERP);
assignin('base','plotset', plotset);

%---------------------------------------------------------------------------------------------------
%                                                                                                   |
%
% EEGLAB import multiple dataset (Biosig MENU)
%
e_try        = 'try,';
e_catch      = 'catch, eeglab_error; LASTCOM= ''''; clear EEGTMP ALLEEGTMP STUDYTMP; end;';
nocheck      = e_try;
storeallcall = [ 'if ~isempty(ALLEEG) & ~isempty(ALLEEG(1).data), ALLEEG = eeg_checkset(ALLEEG);' ...
        'EEG = eeg_retrieve(ALLEEG, CURRENTSET); eegh(''ALLEEG = eeg_checkset(ALLEEG); EEG = eeg_retrieve(ALLEEG, CURRENTSET);''); end;' ];
ifeeg        =  'if ~isempty(LASTCOM) & ~isempty(EEG),';
e_storeall_nh   = [e_catch 'eegh(LASTCOM);' ifeeg storeallcall 'disp(''Done.''); end; eeglab(''redraw'');'];
cb_loaderplabset     = [ nocheck '[ALLEEG EEG CURRENTSET LASTCOM] = pop_loadmerplabset(ALLEEG, EEG);' e_storeall_nh];

%
%  Create menu import multiple datasets
%
menu_import_erplab = findobj(fig, 'tag', 'import data');
uimenu( menu_import_erplab, 'Label', ['Load multiple EEGLAB/ERPLAB ' erplabver ' datasets'],...
        'CallBack', cb_loaderplabset, 'Separator', 'on');
%                                                                                                   |
%---------------------------------------------------------------------------------------------------

%
% ERPLAB NEST-MENU  (ERPLAB at the EEGLAB's Main Menu)
%
if ispc      % windows
      wfactor1 = 1.20;
      wfactor2 = 1.21;
elseif ismac % Mac OSX
      wfactor1 = 1.45;
      wfactor2 = 1.46;
else
      wfactor1 = 1.30;
      wfactor2 = 1.31;
end
posmainfig = get(gcf, 'Position');
hframe     = findobj('parent', gcf, 'tag', 'Frame1');
posframe   = get(hframe, 'position');
set(gcf, 'position', [posmainfig(1:2) posmainfig(3)*wfactor1 posmainfig(4)]);
set(hframe, 'position', [posframe(1:2) posframe(3)*wfactor2 posframe(4)]);

menuERPLAB = findobj(fig, 'tag', 'EEGLAB');   % At EEGLAB Main Menu

%****************************************************************************************************
%****************************************|        MENU      |****************************************
%****************************************|      CALLBACKS   |****************************************
%****************************************************************************************************

%
% ARTIFACT DETECTION FOR CONTINUOUS DATA callback
%
comREJCON = [trystrs.no_check '[EEG LASTCOM] = pop_continuousartdet(EEG);' catchstrs.store_and_hist ];

%
% EVENTLIST callback
%
comCLF1    = [trystrs.no_check '[EEG LASTCOM] = pop_creabasiceventlist(EEG);' catchstrs.new_and_hist ];
%comCLF2    = [trystrs.no_check '[EEG LASTCOM] = pop_editeventlist(EEG);' catchstrs.store_and_hist ]; %ERPLAB 2.0.70 and higher
comSMMRZ   = [trystrs.no_check '[EEG LASTCOM] = pop_squeezevents(EEG);' catchstrs.add_to_hist ];
comSLFeeg  = [trystrs.no_check '[EEG LASTCOM] = pop_exporteegeventlist(EEG);' catchstrs.add_to_hist ];
comRLFeeg  = [trystrs.no_check '[EEG LASTCOM] = pop_importeegeventlist(EEG);' catchstrs.new_and_hist];
comEXRTeeg = [trystrs.no_check '[values LASTCOM] = pop_rt2text(EEG);' catchstrs.add_to_hist];
comEXRTerp = ['[values ERPCOM] = pop_rt2text(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];

%
% BINLISTER callback
%
comCBL    = [trystrs.no_check '[EEG LASTCOM] = pop_binlister(EEG);' catchstrs.store_and_hist]; 
comMEL    = [trystrs.no_check '[EEG LASTCOM] = pop_overwritevent(EEG);' catchstrs.new_and_hist];

%
% BIN-BASED EPOCHING callback
%
comEB     = [trystrs.no_check '[EEG LASTCOM] = pop_epochbin(EEG);' catchstrs.new_and_hist];

%
% EEG CHANNEL OPERATION callback
%
comCHOP   = [trystrs.no_check '[EEG LASTCOM] = pop_eegchanoperator(EEG);' catchstrs.store_and_hist ]; % ERPLAB 1.1.718 and higher

%
% ARTIFACT DETECTION FOR EPOCHED DATA callbacks
%
comAR0     = [trystrs.no_check '[EEG LASTCOM] = pop_artextval(EEG);' catchstrs.new_and_hist]; % Extreme Values
comAR1     = [trystrs.no_check '[EEG LASTCOM] = pop_artmwppth(EEG);' catchstrs.new_and_hist]; % Peak to peak window voltage threshold
comAR3     = [trystrs.no_check '[EEG LASTCOM] = pop_artblink(EEG);' catchstrs.new_and_hist];  % Blink
comAR4     = [trystrs.no_check '[EEG LASTCOM] = pop_artstep(EEG);' catchstrs.new_and_hist];   % Step-like artifacts
comAR6     = [trystrs.no_check '[EEG LASTCOM] = pop_artdiff(EEG);' catchstrs.new_and_hist];   % sample-to-sample diff
comAR7     = [trystrs.no_check '[EEG LASTCOM] = pop_artderiv(EEG);' catchstrs.new_and_hist];  % Rate of change
comAR8     = [trystrs.no_check '[EEG LASTCOM] = pop_artflatline(EEG);' catchstrs.new_and_hist];  % Blocking & flat line

comARSUMM  = [trystrs.no_check '[goodbad histeEF histoflags  LASTCOM] = pop_summary_rejectfields(EEG);' catchstrs.add_to_hist];
comARSUMM2 = [trystrs.no_check '[pr acce rej histoflags  LASTCOM] = pop_summary_AR_eeg_detection(EEG);' catchstrs.add_to_hist];
comARSUMM3 = [trystrs.no_check '[MPD LASTCOM] = getardetection(EEG, 1);' catchstrs.add_to_hist];
comRSTAR   = [trystrs.no_check '[EEG LASTCOM] = pop_resetrej(EEG);' catchstrs.new_and_hist];  % Rate of change

%
% UTILITIES  callbacks
%
comEEC     = [trystrs.no_check '[EEG LASTCOM] = pop_eraseventcodes(EEG);' catchstrs.new_and_hist];
comICOF    = [trystrs.no_check '[EEG LASTCOM] = pop_insertcodeonthefly(EEG);' catchstrs.new_and_hist];
comICLA    = [trystrs.no_check '[EEG LASTCOM] = pop_insertcodearound(EEG);' catchstrs.new_and_hist];
comICTTL   = [trystrs.no_check '[EEG LASTCOM] = pop_insertcodeatTTL(EEG);' catchstrs.new_and_hist];
comShuff   = [trystrs.no_check '[EEG LASTCOM] = pop_eventshuffler(EEG);' catchstrs.new_and_hist];

comEEGBDR  = [trystrs.no_check '[LASTCOM]     = pop_bdfrecovery(EEG);' catchstrs.add_to_hist];
comBCOL    = 'Bcolorerplab' ;
comFCOL    = 'Fcolorerplab' ;
comFS      = 'Seterplabfontsize';
comRECB    = [trystrs.no_check '[EEG LASTCOM] = pop_setcodebit(EEG);' catchstrs.new_and_hist]; 
comEP2CON  = [trystrs.no_check '[EEG LASTCOM] = pop_epoch2continuous(EEG);' catchstrs.new_and_hist]; 

%
% FILTER EEG callbacks
%
comBFCD    = [trystrs.no_check '[EEG LASTCOM] = pop_basicfilter(EEG);' catchstrs.new_and_hist];
comPAS     = [trystrs.no_check '[LASTCOM] = pop_fourieeg(EEG);' catchstrs.add_to_hist];
%comTK1    = [trystrs.no_check '[EEG LASTCOM] = pop_polydetrend(EEG);' catchstrs.new_and_hist ];
comTK2    = [trystrs.no_check '[EEG LASTCOM] = pop_eeglindetrend(EEG);' catchstrs.new_and_hist ];
comTK3    = '[ERP ERPCOM]  = pop_erplindetrend(ERP); [ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);';

%
% ERP processing callbacks
%
comERPBDR    = ['[ERPCOM]     = pop_bdfrecovery(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSLFerp    = ['[ERPCOM]     = pop_exporterpeventlist(ERP);'  '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comRLFerp    = ['[ERP ERPCOM] = pop_importerpeventlist(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comAPP       = ['[ERP ERPCOM] = pop_appenderp(ALLERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comRERPBL    = ['[ERP ERPCOM] = pop_blcerp(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comCERPch    = ['[ERP ERPCOM] = pop_clearerpchanloc(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comAVG       = ['[ERP ERPCOM] = pop_averager(ALLEEG);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comBOP       = ['[ERP ERPCOM] = pop_binoperator(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comCHOP2     = ['[ERP ERPCOM] = pop_erpchanoperator(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comPLOT      = ['[ERPCOM]     = pop_ploterps(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSCALP     = ['[ERPCOM]     = pop_scalplot(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSAVE      = ['[ERP issave ERPCOM]= pop_savemyerp(ERP,''gui'',''save'');' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSAVEas    = ['[ERP issave ERPCOM]= pop_savemyerp(ERP, ''gui'',''saveas'');' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comDUPLI     = ['[ERP issave ERPCOM]= pop_savemyerp(ERP,''gui'',''erplab'');' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSendemail = ['[ERPCOM]= pop_senderpbymail(ALLERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comSetemail  = 'setemailGUI;';

% export ERP
comEXPAVG    = ['[ERPCOM] = pop_erp2asc(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comEXPUNI    = ['[ERPCOM] = pop_export2text(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];

% import ERP
comIMPERP    = ['[ERP ERPCOM]   = pop_importerp;' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comIMPERPSS  = ['[ERP ALLERP ERPCOM] = pop_importerpss; ' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comIMPNEURO  = ['[ERP ERPCOM] = pop_importavg(''''); ' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];

% load ERP
comLDERP   = ['[ERP ALLERP ERPCOM] = pop_loaderp('''');' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comDELERP  = ['[ALLERP ERPCOM]  = pop_deleterpset(ALLERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comGAVG    = ['[ERP ERPCOM]     = pop_gaverager(ALLERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comERPMT   = ['[Amp Lat ERPCOM] = pop_geterpvalues(ALLERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comEXPPDF  = 'pop_fig2pdf;' ;
comhelpman = 'pop_erphelp;' ;
comhelptut = 'pop_erphelptut;' ;
comhelpsrp = 'pop_erphelpscript;' ;

%
% Filter ERP callbacks
%
comFil    = ['[ERP ERPCOM] = pop_filterp(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comPASerp = ['LASTCOM = pop_fourierp(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];

%
% ERP AR summary callback
%
comARSUMerp1 = ['[tacce trej histoflags  ERPCOM] = pop_summary_AR_erp_detection(ERP);' '[ERP ALLERPCOM] = erphistory(ERP, ALLERPCOM, ERPCOM);'];
comARSinc1   = [trystrs.no_check '[EEG LASTCOM]  = pop_syncroartifacts(EEG);' catchstrs.new_and_hist];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        MAIN      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        MENU      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create ERPLAB menu
%
submenu = uimenu( menuERPLAB, 'Label', 'ERPLAB', 'separator','on','tag','ERPLAB');
set(submenu, 'position', 6); % thanks Arno!

%
% Artifact detection in continuous data
%
uimenu( submenu, 'Label', 'Artifact detection in continuous data', 'CallBack', comREJCON);

%
% EVENTLIST for EEG menu and submenu
%
ELmenu = uimenu( submenu, 'Label', 'EventList'  , 'tag','EventList', 'separator', 'on'); %'CallBack', comCLF, 'separator', 'on');
uimenu( ELmenu, 'Label',  '<html>Create <b>EEG</b> EVENTLIST </html>'  , 'CallBack', comCLF1);
% uimenu( ELmenu, 'Label',  '<html>Create <b>EEG</b> EVENTLIST - Basic </html>'  , 'CallBack', comCLF1);
% uimenu( ELmenu, 'Label',  '<html>Create <b>EEG</b> EVENTLIST - Advanced </html>'  , 'CallBack', comCLF2);
uimenu( ELmenu, 'Label',  '<html>Import <b>EEG</b> EVENTLIST from text file </html>'  , 'CallBack', comRLFeeg);
uimenu( ELmenu, 'Label',  '<html>Export <b>EEG</b> EVENTLIST to text file </html>'  , 'CallBack', comSLFeeg);
uimenu( ELmenu, 'Label',  '<html>Summarize current <b>EEG</b> event codes (output at command window) </html>', 'CallBack', comSMMRZ);
mRTs = uimenu( ELmenu,    'Label',  'Export reaction times to text'  , 'tag', 'ReactionTime','ForegroundColor', [0.6 0 0]); % Reaction Times
uimenu( mRTs, 'Label',    '<html>From <b>EEG</b> </html>'  , 'CallBack', comEXRTeeg); % Reaction Times
uimenu( mRTs, 'Label',    '<html>From <b>ERP</b> </html>'  , 'CallBack', comEXRTerp); % Reaction Times

%
% EVENTLIST for ERP submenu
%
uimenu( ELmenu, 'Label',  '<html>Import <b>ERP</b> EVENTLIST from text file </html>'  , 'CallBack', comRLFerp, 'separator', 'on');
uimenu( ELmenu, 'Label',  '<html>Export <b>ERP</b> EVENTLIST to text file </html>'  , 'CallBack', comSLFerp);
uimenu( submenu, 'Label', 'Assign bins (BINLISTER)'     , 'CallBack', comCBL);
uimenu( submenu, 'Label', 'Transfer eventinfo to EEG.event (optional)', 'CallBack', comMEL, 'separator', 'on');
uimenu( submenu, 'Label', 'Extract bin-based epochs', 'CallBack', comEB, 'separator', 'on');

%
% EEG CHANNEL OPERATIONS
%
uimenu( submenu, 'Label', '<html><b>EEG</b> Channel operations </html>'   , 'CallBack', comCHOP, 'separator','on' );

%
% FILTER ERP submenus
%
mFI = uimenu( submenu,    'Label', 'Filter & frequency tools' , 'separator', 'on');
uimenu( mFI,'Label', '<html>Filters for <b>EEG</b> data </html>'  , 'CallBack', comBFCD); %, 'separator', 'on');
uimenu( mFI,'Label', '<html>Plot amplitude spectrum for <b>EEG</b> data </html>'  , 'CallBack', comPAS);
uimenu( mFI, 'Label', '<html><b>EEG</b> Linear detrend </html>'  , 'CallBack', comTK2);
% uimenu( mFI, 'Label', '<html><b>EEG</b> Polynomial detrend (continuous)'  , 'CallBack', comTK1);
uimenu( mFI, 'Label', '<html>Filters for <b>ERP</b> data </html>', 'CallBack', comFil, 'separator','on' );
uimenu( mFI,'Label', '<html>Plot amplitude spectrum for <b>ERP</b> data </html>'  , 'CallBack', comPASerp);
uimenu( mFI, 'Label', '<html><b>ERP</b> Linear detrend </html>'  , 'CallBack', comTK3);


%
% ARTIFACT DETECTION FOR EPOCHED DATA submenus
%
mAR = uimenu( submenu,    'Label', 'Artifact detection in epoched data'  , 'tag','ART','separator','on');
uimenu( mAR, 'Label', 'Simple voltage threshold'  , 'CallBack', comAR0);
uimenu( mAR, 'Label', 'Moving window peak-to-peak threshold'  , 'CallBack', comAR1);
uimenu( mAR, 'Label', 'Blink rejection (alpha version)'  , 'CallBack', comAR3);
uimenu( mAR, 'Label', 'Step-like artifacts'  , 'CallBack', comAR4);
uimenu( mAR, 'Label', 'Sample to sample voltage threshold'  , 'CallBack', comAR6);
uimenu( mAR, 'Label', 'Rate of change -time derivative- (alpha version)'  , 'CallBack', comAR7);
uimenu( mAR, 'Label', 'Blocking & flat line'  , 'CallBack', comAR8);
%
% ARTIFACT DETECTION summaries submenus
%
mARS = uimenu( mAR,    'Label', '<html>Summarize <b>EEG</b> artifact detection'  , 'tag','ART','separator','on');
uimenu( mARS, 'Label', 'In one value'  , 'CallBack', comARSUMM3, 'ForegroundColor', [0 0 0.6]);
uimenu( mARS, 'Label', 'Table'  , 'CallBack', comARSUMM2, 'ForegroundColor', [0 0 0.6]);
uimenu( mARS, 'Label', 'Graphic'  , 'CallBack', comARSUMM, 'ForegroundColor', [0 0 0.6]);
uimenu( mAR,  'Label', '<html>Clear artifact detection marks on <b>EEG</b> </html>'  , 'CallBack', comRSTAR, 'ForegroundColor', [0.6 0 0]);
uimenu( mAR,  'Label', '<html><b>ERP</b> artifact detection summary table </html>'  , 'CallBack', comARSUMerp1, 'ForegroundColor', [0 0 0.6], 'separator','on');
uimenu( mAR,  'Label', '<html>Synchronize artifact info in <b>EEG</b> and EVENTLIST </html>'  , 'CallBack', comARSinc1, 'separator','on');

%
% AVERAGE ERP
%
uimenu( submenu, 'Label', '<html>Compute averaged <b>ERP</b>s </html>'   , 'CallBack', comAVG, 'separator', 'on');

%
% ERP OPERATIONS submenus
%
mERPOP = uimenu( submenu,    'Label', '<html><b>ERP</b> Operations'  , 'tag','ERPop','separator','on');
uimenu( mERPOP, 'Label', '<html><b>ERP</b> Bin operations </html>'   , 'CallBack', comBOP );
uimenu( mERPOP, 'Label', '<html><b>ERP</b> Channel operations </html>'   , 'CallBack', comCHOP2);
uimenu( mERPOP, 'Label',  '<html>Append <b>ERP</b>sets </html>'  , 'CallBack', comAPP);%, 'separator','on');
uimenu( mERPOP, 'Label',  '<html>Remove <b>ERP</b> baseline </html>'  , 'CallBack', comRERPBL, 'separator','on');

%
% PLOT ERP WAVEFORMS AND MAPS
%
uimenu( submenu, 'Label', '<html>Plot <b>ERP</b> waveforms </html>'   , 'CallBack', comPLOT, 'separator','on');
uimenu( submenu, 'Label', '<html>Plot <b>ERP</b> scalp maps </html>'   , 'CallBack', comSCALP);
uimenu( submenu, 'Label',  '<html>Close all <b>ERPLAB</b> figures </html>'  , 'CallBack', 'clerpf');

%
% EXPORT & IMPORT ERP submenus
%
mEXERP = uimenu( submenu,    'Label', '<html>Export & Import <b>ERP</b>'  , 'tag','Exerp','separator','on');
uimenu( mEXERP, 'Label', '<html>Export <b>ERP</b> to text (readable by ERPSS) </html>', 'CallBack', comEXPAVG);
uimenu( mEXERP, 'Label', '<html>Export <b>ERP</b> to text (universal) </html>', 'CallBack', comEXPUNI);
uimenu( mEXERP, 'Label', '<html>Import <b>ERP</b> from text (ERPSS) </html>', 'CallBack', comIMPERPSS, 'separator', 'on'); 
uimenu( mEXERP, 'Label', '<html>Import <b>ERP</b> from Neuroscan (*.avg) </html>'  , 'CallBack', comIMPNEURO);
uimenu( mEXERP, 'Label', '<html>Import <b>ERP</b> from text (universal) </html>'  , 'CallBack', comIMPERP);
uimenu( mEXERP, 'Label', 'Export plotted figure to PDF/EPS', 'CallBack', comEXPPDF, 'separator', 'on');

%
% LOAD & SAVE ERPset(s)
%
uimenu( submenu, 'Label', '<html>Load existing <b>ERP</b>set </html>', 'CallBack', comLDERP, 'separator', 'on');
uimenu( submenu, 'Label', '<html>Clear <b>ERP</b>set(s) </html>', 'CallBack', comDELERP);
uimenu( submenu, 'Label', '<html>Save current <b>ERP</b>set </html>'   , 'CallBack', comSAVE);
uimenu( submenu, 'Label', '<html>Save current <b>ERP</b>set as </html>'   , 'CallBack', comSAVEas);
uimenu( submenu, 'Label', '<html>Duplicate or rename current <b>ERP</b>set </html>'   , 'CallBack', comDUPLI);
uimenu( submenu, 'Label', '<html>Send <b>ERP</b>set by e-mail </html>'   , 'CallBack', comSendemail);

%
% MEASUREMENT TOOL
%
uimenu( submenu, 'Label', '<html><b>ERP</b> Measurement tool </html>', 'CallBack', comERPMT, 'separator', 'on');

%
% GRAND AVERAGE
%
uimenu( submenu, 'Label', '<html>Average across <b>ERP</b>sets (Grand Average) </html>', 'CallBack', comGAVG, 'separator', 'on');

%
% UTILITIES submenus
%
mUTI = uimenu( submenu, 'Label', 'Utilities'  , 'tag','Utilities', 'separator', 'on');
uimenu( mUTI, 'Label',  'Convert an epoched dataset into a continuous one'  , 'CallBack', comEP2CON);
mINS = uimenu( mUTI,    'Label',  'Insert event codes'  , 'tag', 'insertcodes',  'separator', 'on');
uimenu( mINS, 'Label',  '<html>Insert event codes using threshold (continuous <b>EEG</b>) </html>'  , 'CallBack', comICOF);
uimenu( mINS, 'Label',  '<html>Insert event codes using latency(ies) (continuous <b>EEG</b>) </html>'  , 'CallBack', comICLA);
uimenu( mINS, 'Label',  '<html>Insert event codes at TTL onsets (continuous <b>EEG</b>) </html>'  , 'CallBack', comICTTL);
uimenu( mUTI, 'Label',  '<html>Erase undesired event codes (continuous <b>EEG</b>) </html>'  , 'CallBack', comEEC, 'separator','on');
uimenu( mUTI, 'Label',  '<html>Shuffle events/bins (continuous <b>EEG</b>) </html>'  , 'CallBack', comShuff, 'separator','on');
uimenu( mUTI, 'Label',  '<html>Recover bin descriptor file from <b>EEG</b> </html>'  , 'CallBack', comEEGBDR, 'separator','on');
uimenu( mUTI, 'Label',  '<html>Recover bin descriptor file from <b>ERP</b> </html>'  , 'CallBack', comERPBDR);%, 'separator','on');
uimenu( mUTI, 'Label',  '<html>Clear <b>ERP</b> channel location info </html>'  , 'CallBack', comCERPch, 'separator', 'on');
uimenu( mUTI, 'Label',  'Reset event code bytes'  , 'CallBack', comRECB,  'separator', 'on');uimenu( mUTI, 'Label',  '<html><FONT color="blue" >Find more here! <FONT color="black">(for scripting) </html>', 'CallBack', 'web(''http://www.erpinfo.org/erplab/erplab-documentation/utilities/view'',''-browser'');', 'separator', 'on');

%
% Settings submenus
%
mSETT = uimenu( submenu, 'Label', 'Settings'  , 'tag','Settings', 'separator', 'on');
uimenu( mSETT, 'Label',  'Set font size for ERPLAB''s GUIs', 'CallBack', comFS);
uimenu( mSETT, 'Label',  'Edit ERPLAB''s completion statement'  , 'CallBack', 'msg2endGUI',  'separator', 'on');
uimenu( mSETT, 'Label',  '<html>Set <b>Gmail</b> or <b>Yahoo</b> e-mail account </html>'   , 'CallBack', comSetemail,  'separator', 'on');
uimenu( mSETT, 'Label',  '<html>ERPLAB <FONT color="red" >Bac<FONT color="green" >kgro<FONT color="blue" >und <FONT color="black">Color </html>', 'CallBack', comBCOL, 'separator','on');
uimenu( mSETT, 'Label',  '<html>ERPLAB <FONT color="red" >For<FONT color="green" >egro<FONT color="blue" >und <FONT color="black">Color </html>', 'CallBack', comFCOL);
uimenu( mSETT, 'Label',  'Reset ERPLAB''s working memory'  , 'CallBack', 'erplabamnesia(1)',  'separator', 'on');
uimenu( mSETT, 'Label',  'Backup this ERPLAB version'  , 'CallBack', 'backuperplab',  'separator', 'on');

%
% SUPPORT
%
mhelp = uimenu( submenu,  'Label', 'Help', 'tag','erphelp', 'separator', 'on');
uimenu( mhelp, 'Label',   'ERPLAB Manual', 'CallBack', comhelpman);
uimenu( mhelp, 'Label',   'ERPLAB Tutorial', 'CallBack', comhelptut);
uimenu( mhelp, 'Label',   'ERPLAB Scripting', 'CallBack', comhelpsrp);
uimenu( mhelp, 'Label',   '<html><FONT color="blue" >E-mail Us </html>', 'CallBack', 'web(''mailto:erplabtoolbox@gmail.com'');', 'separator', 'on');
uimenu( submenu, 'Label', 'About ERPLAB', 'CallBack', 'abouterplabGUI', 'separator', 'on');

%
% CREATE ERPset MAIN MENU
%
erpmenu = uimenu( menuERPLAB, 'Label', 'ERPsets', 'separator','on','tag','erpsets');
set(erpmenu, 'position', 7);
set(erpmenu, 'enable', 'off');
