0001 function [signal, accept, typeeeg, rt, response, chan_names, pnts, nsweeps, rate, xmin, xmax]=loadeeg( FILENAME, chanlist, TrialList, typerange, acceptype, rtrange, responsetype)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 if nargin<1
0032 fprintf('Not enought arguments\n');
0033 help loadeeg
0034 return;
0035 end;
0036 if nargin<2 CHAN='all'; end;
0037 if nargin<3 TrialList='all'; end;
0038 if nargin<4 typerange='all'; end;
0039 if nargin<5 acceptype='all'; end;
0040 if nargin<6 rtrange ='all'; end;
0041 if nargin<7 responsetype='all'; end;
0042
0043
0044
0045 fid=fopen(FILENAME,'r','ieee-le');
0046 if fid<0
0047 fprintf(2,['Error LOADEEG: File ' FILENAME ' not found\n']);
0048 return;
0049 end;
0050
0051
0052
0053 erp = fread(fid, 362, 'uchar');
0054 nsweeps = fread(fid, 1, 'ushort');
0055 erp = fread(fid, 4, 'uchar');
0056 pnts= fread(fid, 1, 'ushort');
0057 chan= fread(fid, 1, 'ushort');
0058 erp = fread(fid, 4, 'uchar');
0059 rate= fread(fid, 1, 'ushort');
0060 erp = fread(fid, 127, 'uchar');
0061 xmin= fread(fid, 1, 'float32');
0062 xmax= fread(fid, 1, 'float32');
0063 erp = fread(fid, 387, 'uchar');
0064
0065 fprintf('number of channels : %d\n', chan);
0066 fprintf('number of points per trial : %d\n', pnts);
0067 fprintf('sampling rate (Hz) : %f\n', rate);
0068 fprintf('xmin (s) : %f\n', xmin);
0069 fprintf('xmax (s) : %f\n', xmax);
0070
0071
0072
0073 fprintf('Electrode configuration\n');
0074 for elec = 1:chan
0075 channel_label_tmp = fread(fid, 10, 'uchar');
0076 chan_names(elec,:) = channel_label_tmp';
0077 for index = 2:9 if chan_names(elec,index) == 0 chan_names(elec,index)=' '; end; end;
0078 erp = fread(fid, 47-10, 'uchar');
0079 baseline(elec) = fread(fid, 1, 'ushort');
0080 erp = fread(fid, 10, 'uchar');
0081 sensitivity(elec) = fread(fid, 1, 'float32');
0082 erp = fread(fid, 8, 'uchar');
0083 calib(elec) = fread(fid, 1, 'float32');
0084 fprintf('%s: baseline: %d\tsensitivity: %f\tcalibration: %f\n', chan_names(elec,1:4), baseline(elec), sensitivity(elec), calib(elec));
0085 factor(elec) = calib(elec) * sensitivity(elec) / 204.8;
0086 end;
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 xsize = chan * pnts;
0100 buf_size = chan * pnts ;
0101
0102
0103
0104 if size(chanlist) == size('all') chanlist = [1:chan]; end;
0105 if size(TrialList) == size('all') trialtagI = 1; else trialtagI = 0; end;
0106 if size(acceptype) == size('all') acceptagI = 1; else acceptagI = 0; end;
0107 if size(typerange) == size('all') typetagI = 1; else typetagI = 0; end;
0108 if size(responsetype) == size('all') responsetagI = 1; else responsetagI = 0; end;
0109 if size(rtrange) == size('all') rttagI = 1; else rttagI = 0; end;
0110
0111 count_selected = 1;
0112 fprintf('Reserving array (can take some time)\n');
0113 signal = zeros( chan, pnts*nsweeps);
0114 fprintf('Array reserved, scanning file\n');
0115
0116 for sweep = 1:nsweeps
0117
0118
0119
0120 s_accept = fread(fid, 1, 'uchar');
0121 s_type = fread(fid, 1, 'ushort');
0122 s_correct = fread(fid, 1, 'ushort');
0123 s_rt = fread(fid, 1, 'float32');
0124 s_response = fread(fid, 1, 'ushort');
0125 s_reserved = fread(fid, 1, 'ushort');
0126
0127 unreaded_buf = 1;
0128
0129
0130
0131 if trialtagI trialtag = 1; else trialtag = ismember(sweep, TrialList); end;
0132 if acceptagI acceptag = 1; else acceptag = ismember(s_accept, acceptype); end;
0133 if typetagI typetag = 1; else typetag = ismember(s_type, typerange); end;
0134 if responsetagI responsetag = 1; else responsetag = ismember(s_response, responsetype); end;
0135 if rttagI rttag = 1; else rttag = ismember(s_rt, rtrange); end;
0136
0137 if typetag
0138 if trialtag
0139 if acceptag
0140 if responsetag
0141 if rttag
0142
0143 buf = fread(fid, [chan pnts], 'short');
0144 unreaded_buf = 0;
0145
0146
0147
0148 accept(count_selected) = s_accept;
0149 typeeeg(count_selected) = s_type;
0150 rt(count_selected) = s_rt;
0151 response(count_selected) = s_response;
0152
0153
0154
0155 for elec = 1:chan
0156 buf(elec, :) = (buf(elec, :)-baseline(elec)-0.0)*factor(elec);
0157 end;
0158 signal(:,[((count_selected-1)*pnts+1):count_selected*pnts]) = buf;
0159 count_selected = count_selected + 1;
0160 if not(mod(count_selected,10)) fprintf('%d sweeps selected out of %d\n', count_selected-1, sweep); end;
0161 end;
0162 end;
0163 end;
0164 end;
0165 end;
0166
0167 if unreaded_buf fseek(fid, buf_size*2, 'cof'); end;
0168 end;
0169 nsweeps = count_selected-1;
0170 fclose(fid);
0171
0172
0173
0174 fprintf('rereservation of variables\n');
0175 signal = signal(chanlist, 1:(count_selected-1)*pnts);
0176 chan_names = chan_names(chanlist,:);
0177
0178 return;
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207