%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DALF was created by Timothy J Smale tim_smale@hotmail.com       %
%   for Nottingham Trent university                              %
%   and Nottingham University School of Biomedical Sciences      %
%   2004/2005                                                    %
%                                                                %
%version 1.0 - 15/02/05                                          %
%                                                                %
%version 1.01 - 16/02/05                                         %
%      eeglab button added (TS)                                  %
%                                                                %
%                                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

DALF_VERSION='1.01';

backcolour=[0.7529 1 0.7529];
edittextcolour=[1 1 1];
figure('name', [ 'DALF v' DALF_VERSION ],'color',backcolour,...
    'numbertitle', 'off','resize', 'off');
 
if exist('V1g')==0,
    V1g='AD01';
end;

if exist('V2g')==0,
    V2g='AD02';
end;

if exist('V3g')==0,
    V3g='AD03';
end;

if exist('V4g')==0,
    V4g='AD04';
end;

if exist('V5g')==0,
    V5g='AD05';
end;

if exist('V6g')==0,
    V6g='AD06';
end;

if exist('V7g')==0,
    V7g='AD07';
end;

if exist('V8g')==0,
    V8g='AD08';
end;

if exist('Vng')==0,
    Vng='1';
end;

if exist('opmg')==0,
    opmg='LFP';
end;

if exist('edg')==0,
    edg='Event002';
end;

if exist('edog')==0,
    edog='EVENT';
end;

if exist('eventyn_use')==0,
    eventyn_use=0;
end;

if exist('event_preset_use')==0,
    event_preset_use=1;
end;

if exist('edg2')==0,
    edg2='Event003';
end;

if exist('eventyn_use2')==0,
    eventyn_use2=0;
end;


concat=[];
lngeventcat=[];
eventcat=[];
lngeventcat2=[];
eventcat2=[];
eventcat3=[];
eventcat4=[];
runningevent=[];
apost='''';
preset_event=[0.01 1];
preset_event2=[0.01 2];

% Callback function

exit=['clear V1 V1g V1s V2 V2g V2s V3 V3g V3s V4 V4g V4s V5 V5g;'...
    'clear V5s V6 V6g V6s V7 V7g V7s V8 V8g V8s Vn Vng Vns apost;'...
    'clear concat exit opm opmg opms runcon event_use eventyn;'...
    'clear eyn_func h ed edg edo edog edos eds event_length event_preset;'...
    'clear event_preset_func event_preset_use eventcat eventyn_use;'...
    'clear lngeventcat preset_event repli_event runconsec runconset;'...
    'clear ed2 edg2 edo2 edos2 eds2 event_preset2 event_preset_func2;'...
    'clear event_preset_use2 eventcat2 eventyn2 eventyn_use2 eyn_func2;'...
    'clear lngeventcat2 partx party preset_event2 runconset2 runconthi;'...
    'clear runningevent eventopen1 open1 eventopen2 open2 eventcat3;'...
    'clear eventcat4 backcolour edittextcolour DALF_VERSION runeeg; close;'];

V1s='V1g=get(V1,''string'');';
V2s='V2g=get(V2,''string'');';
V3s='V3g=get(V3,''string'');';
V4s='V4g=get(V4,''string'');';
V5s='V5g=get(V5,''string'');';
V6s='V6g=get(V6,''string'');';
V7s='V7g=get(V7,''string'');';
V8s='V8g=get(V8,''string'');';
Vns='Vng=get(Vn,''string'');';
opms='opmg=get(opm,''string'');';
eds='edg=get(ed,''string'');';
edos='edog=get(edo,''string'');';
eyn_func='eventyn_use=get(eventyn,''value'');';
event_preset_func='event_preset_use=get(event_preset,''value'');';
eds2='edg2=get(ed2,''string'');';
eyn_func2='eventyn_use2=get(eventyn2,''value'');';

eventopen1=['if event_preset_use==1,'...
                'msgbox(''Editing is not required with option 1'',''Note Option 1 Edit'',''warn'');'...
            'elseif event_preset_use==2,'...
                'msgbox(''Edit column 1 only - Event latency (Seconds)'',''Note Option 2 Edit'',''warn'');'...
                'open1=cat(2,''openvar '',edg); eval(open1);'...
            'elseif event_preset_use==3,'...
                'msgbox(''Column 1 - Event latency (Seconds)                                    Column 2 - Event duration (Seconds)                                     Column 3 - Event type (1,2,3,etc.)'',''Note Option 3 Edit'',''warn'');'...
                'open1=cat(2,''openvar '',edg); eval(open1);'...
                'end;'];

eventopen2=['if event_preset_use==1,'...
                'msgbox(''Editing is not required with option 1'',''Note Option 1 Edit'',''warn'');'...
            'elseif event_preset_use==2,'...
                'msgbox(''Edit column 1 only - Event latency (Seconds)'',''Note Option 2 Edit'',''warn'');'...
                'open2=cat(2,''openvar '',edg2); eval(open2);'...
            'elseif event_preset_use==3,'...
                'msgbox(''Column 1 - Event latency (Seconds)                                    Column 2 - Event duration (Seconds)                                     Column 3 - Event type (1,2,3,etc.)'',''Note Option 3 Edit'',''warn'');'...
                'open2=cat(2,''openvar '',edg2); eval(open2);'...
                'end;'];

runconset=['if event_preset_use==1 || event_preset_use==2,'...
                'lngeventcat=cat(2,''event_length=length('',edg,'');''); eval(lngeventcat);'...
                'repli_event=repmat(preset_event,event_length,1);'...
                'eventcat=cat(2,edog,''=cat(2,'',edg,'',repli_event);''); eval(eventcat);'...
           'elseif event_preset_use==3,'...
                'eventcat3=cat(2,edog,''='',edg,'';''); eval(eventcat3);'...
           'end;'];

runconset2=['if event_preset_use==1 || event_preset_use==2,'...
                'lngeventcat=cat(2,''event_length=length('',edg,'');''); eval(lngeventcat);'...
                'repli_event=repmat(preset_event,event_length,1);'...
                'eventcat=cat(2,''partx=cat(2,'',edg,'',repli_event);''); eval(eventcat);'...
                'lngeventcat2=cat(2,''event_length=length('',edg2,'');''); eval(lngeventcat2);'...
                'repli_event=repmat(preset_event2,event_length,1);'...
                'eventcat2=cat(2,''party=cat(2,'',edg2,'',repli_event);''); eval(eventcat2);'...
                'runningevent=cat(2,edog,''=cat(1,partx,party);''); eval(runningevent);'...
           'elseif event_preset_use==3,'...
                'eventcat4=cat(2,edog,''=cat(1,'',edg,'','',edg2,'');''); eval(eventcat4);'...
           'end;'];
                
runconthi=['if eventyn_use2==0,', runconset ,';'...
           'elseif eventyn_use2==1,', runconset2 ,'end;'];
           
runconsec=['if eventyn_use==1,', runconthi ,' end;'];

runcon=['if Vng==''1'', concat=cat(2,opmg,''=cat(2,'',V1g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''2'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''3'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''4'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'','',V4g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''5'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'','',V4g,'','',V5g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''6'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'','',V4g,'','',V5g,'','',V6g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''7'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'','',V4g,'','',V5g,'','',V6g,'','',V7g,'')'',apost,'';''); eval(concat);'...
        'elseif Vng==''8'', concat=cat(2,opmg,''=cat(2,'',V1g,'','',V2g,'','',V3g,'','',V4g,'','',V5g,'','',V6g,'','',V7g,'','',V8g,'')'',apost,'';''); eval(concat);'...
        'else, disp(''ERROR - illegal number of variables''); end;', runconsec];

runeeg=['eeglab'];



%Frames
%lfp frame
uicontrol('style','frame','units','normal','pos',[.04 .35 .33 .6],'Backgroundcolor',backcolour);
%event frames
uicontrol('style','frame','units','normal','pos',[.52 .3 .46 .65],'Backgroundcolor',backcolour);

%LOGO
uicontrol('style','text','units','normal','pos',[.14 .1 .3 .07],...
        'string',[ 'DALF v' DALF_VERSION ],'fontsize',20,'foregroundcolor',[0 0 1],'Backgroundcolor',backcolour);


% Push buttons

uicontrol('style','push','units','normal','pos',[.01 .02 .08 .06],...
        'string','Exit','Call',exit);
uicontrol('style','push','units','normal','pos',[.71 .02 .08 .06],...
        'string','RUN','Call',runcon,'Backgroundcolor',[1 0 0]);
uicontrol('style','push','units','normal','pos',[.87 .02 .1 .06],...
        'string','EEGLAB','Call',runeeg);
uicontrol('style','push','units','normal','pos',[.55 .73 .08 .06],...
        'string','EDIT','Call',eventopen1);
uicontrol('style','push','units','normal','pos',[.55 .51 .08 .06],...
        'string','EDIT','Call',eventopen2);
    
% Editable text

opm=uicontrol('style','edit','units','normal','pos',[.05 .4 .08 .04],...
        'string',opmg,'Call',opms,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .38 .16 .07],...
        'string','Output channel (variable) Name','Backgroundcolor',backcolour);

V1=uicontrol('style','edit','units','normal','pos',[.05 .8 .08 .04],...
        'string',V1g,'Call',V1s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .8 .05 .035],...
        'string','LFP1','Backgroundcolor',backcolour);

V2=uicontrol('style','edit','units','normal','pos',[.05 .7 .08 .04],...
        'string',V2g,'Call',V2s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .7 .05 .035],...
        'string','LFP2','Backgroundcolor',backcolour);
    
V3=uicontrol('style','edit','units','normal','pos',[.05 .6 .08 .04],...
        'string',V3g,'Call',V3s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .6 .05 .035],...
        'string','LFP3','Backgroundcolor',backcolour);
    
V4=uicontrol('style','edit','units','normal','pos',[.05 .5 .08 .04],...
        'string',V4g,'Call',V4s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .5 .05 .035],...
        'string','LFP4','Backgroundcolor',backcolour);
    
V5=uicontrol('style','edit','units','normal','pos',[.22 .8 .08 .04],...
        'string',V5g,'Call',V5s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.31 .8 .05 .035],...
        'string','LFP5','Backgroundcolor',backcolour);
    
V6=uicontrol('style','edit','units','normal','pos',[.22 .7 .08 .04],...
        'string',V6g,'Call',V6s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.31 .7 .05 .035],...
        'string','LFP6','Backgroundcolor',backcolour);

V7=uicontrol('style','edit','units','normal','pos',[.22 .6 .08 .04],...
        'string',V7g,'Call',V7s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.31 .6 .05 .035],...
        'string','LFP7','Backgroundcolor',backcolour);

V8=uicontrol('style','edit','units','normal','pos',[.22 .5 .08 .04],...
        'string',V8g,'Call',V8s,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.31 .5 .05 .035],...
        'string','LFP8','Backgroundcolor',backcolour);
    
Vn=uicontrol('style','edit','units','normal','pos',[.05 .9 .08 .04],...
        'string',Vng,'Call',Vns,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.14 .9 .21 .035],...
        'string','Number of LFP channels','Backgroundcolor',backcolour);

ed=uicontrol('style','edit','units','normal','pos',[.55 .83 .11 .05],...
        'string',edg,'Call',eds,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.66 .81 .18 .07],...
        'string','1 Event data input channel (variable)','Backgroundcolor',backcolour);
    
ed2=uicontrol('style','edit','units','normal','pos',[.55 .61 .11 .05],...
        'string',edg2,'Call',eds2,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.66 .59 .18 .07],...
        'string','2 Event data input channel (variable)','Backgroundcolor',backcolour);
    
edo=uicontrol('style','edit','units','normal','pos',[.59 .35 .1 .05],...
        'string',edog,'Call',edos,'Backgroundcolor',edittextcolour);
uicontrol('style','text','units','normal','pos',[.7 .33 .18 .07],...
        'string','Event data output channel (variable)','Backgroundcolor',backcolour);


    
%Radio buttons


eventyn=uicontrol('style','radio','units','normal',...
    'pos',[.55 .9 .22 .04],'string','Include data events?',...
    'value',eventyn_use,'call',eyn_func,'Backgroundcolor',backcolour);

eventyn2=uicontrol('style','radio','units','normal',...
    'pos',[.55 .68 .29 .04],'string','Include second data events?',...
    'value',eventyn_use2,'call',eyn_func2,'Backgroundcolor',backcolour);

%Popup menus

event_preset=uicontrol('style','popupmenu','units','normal',...
    'pos',[.55 .45 .4 .04],'string','1 Set all event durations equal to 10mS|2 Input / edit individual event latency|3 Edit event latency duration and type',...
    'value',event_preset_use,'Call',event_preset_func,'Backgroundcolor',backcolour);