1function [HDR] = eload(filename,Fs)
2% ELOAD loads EVENT data
3% Event information is often stored in different formats.
4% ELOAD tries to load different formats into a unified
5% form
6%
7% HDR = eload(filename)
8%
9% filename	Filename of Event information
10% HDR.EVENT contains the EVENT information
11%
12%
13% see also: SLOAD, SVIEW, SOPEN
14%
15
16
17%  Copyright (C) 1997-2004,2020 by Alois Schloegl <alois.schloegl@gmail.com>
18%    This is part of the BIOSIG-toolbox https://biosig.sourceforge.io/
19
20% This library is free software; you can redistribute it and/or
21% modify it under the terms of the GNU Library General Public
22% License as published by the Free Software Foundation; either
23% Version 2 of the License, or (at your option) any later version.
24%
25% This library is distributed in the hope that it will be useful,
26% but WITHOUT ANY WARRANTY; without even the implied warranty of
27% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28% Library General Public License for more details.
29%
30% You should have received a copy of the GNU Library General Public
31% License along with this library; if not, write to the
32% Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33% Boston, MA  02111-1307, USA.
34
35
36HDR = getfiletype(filename);
37
38if strcmp(HDR.TYPE,'MAT')
39        tmp = load('-mat',filename);
40        if isfield(tmp,'eventmatrix') & isfield(tmp,'samplerate')
41                %%% F. Einspieler's Event information
42                HDR.EVENT.POS = tmp.eventmatrix(:,1);
43                HDR.EVENT.TYP = tmp.eventmatrix(:,2);
44                HDR.EVENT.CHN = tmp.eventmatrix(:,3);
45                HDR.EVENT.DUR = tmp.eventmatrix(:,4);
46                HDR.EVENT.Fs  = tmp.samplerate;
47                HDR.TYPE = 'EVENT';
48
49        elseif isfield(tmp,'EVENT')
50                HDR.EVENT = EVENT;
51                HDR.TYPE = 'EVENT';
52        end;
53
54elseif strcmp(HDR.TYPE,'GDF');
55        H = sopen(HDR,'r'); H=sclose(H);
56        HDR.EVENT = H.EVENT;
57        HDR.EVENT.Fs = H.SampleRate;
58        HDR.TYPE = 'EVENT';
59
60elseif strncmp(HDR.TYPE,'BrainVision',11);
61        HDR = sopen(HDR,'r'); HDR=sclose(HDR);
62	if isfield(HDR.EVENT,'TeegType')
63		ix = strmatch('New Segment',HDR.EVENT.TeegType);
64		HDR.EVENT.TYP(ix)=hex2dec('7ffe');
65	end;
66	for k1 = 1:length(HDR.EVENT.Desc)
67		tmp = HDR.EVENT.Desc{k1};
68		%HDR.TRIG = HDR.EVENT.POS(HDR.EVENT.TYP<10);
69		if 0,
70
71	        elseif strncmp(tmp,'TargetCode',10)
72	        	HDR.EVENT.TYP(k1) = str2double(tmp(11:12))+hex2dec('0300');
73	        elseif strcmp(tmp,'BeginOfTrial')
74	        	HDR.EVENT.TYP(k1) = hex2dec('0300');
75                elseif strcmp(tmp,'hit')
76	        	HDR.EVENT.TYP(k1) = hex2dec('0381');
77	        elseif strcmp(tmp,'wrong')
78	        	HDR.EVENT.TYP(k1) = hex2dec('0382');
79
80	% eye movements
81	        elseif strcmpi(tmp,'augen links')
82	        	HDR.EVENT.TYP(k1) = hex2dec('0431');
83	        elseif strcmpi(tmp,'augen rechts')
84	        	HDR.EVENT.TYP(k1) = hex2dec('0432');
85	        elseif strcmpi(tmp,'augen hoch') | strcmpi(tmp,'augen oben')
86	        	HDR.EVENT.TYP(k1) = hex2dec('0433');
87	        elseif strcmpi(tmp,'augen unten') | strcmpi(tmp,'augen runter')
88	        	HDR.EVENT.TYP(k1) = hex2dec('0434');
89	        elseif strcmpi(tmp,'augen offen')
90	        	HDR.EVENT.TYP(k1) = hex2dec('8430');
91	        elseif strcmpi(tmp,'augen zu')
92	        	HDR.EVENT.TYP(k1) = hex2dec('0430');
93	        elseif strcmp(tmp,'blinzeln')
94	        	HDR.EVENT.TYP(k1) = hex2dec('0439');
95
96	% muscle movements
97	        elseif strcmp(tmp,'EMG links')
98	        	HDR.EVENT.TYP(k1) = hex2dec('0441');
99	        elseif strcmp(tmp,'EMG rechts')
100	        	HDR.EVENT.TYP(k1) = hex2dec('0442');
101	        elseif strcmpi(tmp,'kopf bewegen')
102	        	HDR.EVENT.TYP(k1) = hex2dec('0443');
103	        elseif strcmp(tmp,'zunge an')
104	        	HDR.EVENT.TYP(k1) = hex2dec('0444');
105	        elseif strcmp(tmp,'Kiefer anspannen')
106	        	HDR.EVENT.TYP(k1) = hex2dec('0445');
107	        elseif strcmp(tmp,'zunge aus')
108	        	HDR.EVENT.TYP(k1) = hex2dec('8444');
109	        elseif strcmp(tmp,'kopf beißen') | strcmp(tmp,'kopf beißen'),
110	        	HDR.EVENT.TYP(k1) = hex2dec('0446');
111	        elseif strcmp(tmp,'EMG fuss')
112	        	HDR.EVENT.TYP(k1) = hex2dec('0447');
113	        elseif strcmp(tmp,'Arme bewegen')
114	        	HDR.EVENT.TYP(k1) = hex2dec('0449');
115
116	        elseif strncmp(tmp,'S',1)
117	        	n = str2double(tmp(2:end));
118			if n==11,	% hit (left)
119			       	HDR.EVENT.TYP(k1) = hex2dec('0381');
120			elseif n==12,	% hit (right)
121		        	HDR.EVENT.TYP(k1) = hex2dec('0381');
122	        	elseif n==21,	% miss (left)
123		        	HDR.EVENT.TYP(k1) = hex2dec('0382');
124			elseif n==22,	% miss (right)
125		        	HDR.EVENT.TYP(k1) = hex2dec('0382');
126			elseif n==60,	% feedback onset
127		        	HDR.EVENT.TYP(k1) = hex2dec('030d');
128			else
129		        	HDR.EVENT.TYP(k1) = n;
130			end;
131
132	        elseif strcmp(tmp,'s') | strcmp(tmp,'stop') | strcmp(tmp,'stopp'),
133	        	HDR.EVENT.TYP(k1) = bitxor(hex2dec('8300'),HDR.EVENT.TYP(k1-1));
134
135	        elseif ~isempty(tmp)
136			[n,v,s] = biosig_str2double(tmp(2:end));
137	        	if (length(n)==1) & (~v)
138	        		HDR.EVENT.TYP(k1) = n;
139	       		end;
140	        end;
141	end;
142	HDR.EVENT.TYP = HDR.EVENT.TYP(:);
143
144	if isfield(HDR.EVENT,'POS');
145	       	ix1 = find(HDR.EVENT.TYP<10);
146	       	ix2 = find(HDR.EVENT.TYP==100);
147		HDR.EVENT.TYP(ix2,1) = HDR.EVENT.TYP(ix2-1)+hex2dec('8000');
148		ix0 = find((HDR.EVENT.TYP>0)&(HDR.EVENT.TYP<10));
149		HDR.TRIG = HDR.EVENT.POS(ix0);
150		HDR.Classlabel = HDR.EVENT.TYP(ix0);
151	end;
152
153        HDR = bv2biosig_events(H);
154
155        %%% Artifact database of the sleep EEG
156elseif strcmp(HDR.FILE.Ext,'txt') & strmatch(HDR.FILE.Name,['h000201';'h000901';'h001001']);
157        HDR.EVENT = adb2event(filename,100);
158        HDR.TYPE = 'EVENT';
159elseif strcmp(HDR.FILE.Ext,'txt') & strmatch(HDR.FILE.Name,['b000101';'b000401';'c000701';'c001701';'m000401';'m000901']);
160        HDR.EVENT = adb2event(filename,200);
161        HDR.TYPE = 'EVENT';
162elseif strcmp(HDR.FILE.Ext,'txt') & strmatch(HDR.FILE.Name,['n000101';'n000401';'p000101';'p000201';'s000201']);
163        HDR.EVENT = adb2event(filename,256);
164        HDR.TYPE = 'EVENT';
165elseif strcmp(HDR.FILE.Ext,'txt') & strmatch(HDR.FILE.Name,['u000601']);
166        HDR.EVENT = adb2event(filename,400);
167        HDR.TYPE = 'EVENT';
168
169elseif strcmp(HDR.TYPE,'WSCORE_EVENT')
170        %HDR.EVENT.POS = HDR.EVENT.POS;         % already defined
171        HDR.EVENT.TYP = HDR.EVENT.WSCORETYP;         % code assignment not
172        fprintf(2,'Warning ELOAD: Event Codes in file %s do not not follow the standard codes of BIOSIG.\n',filename);
173        %defined
174
175else
176        fprintf(2,'Warning ELOAD: file %s is not recognized as event file.\n',filename);
177
178end;
179