0001 function [data,times,ch_names] = mne_ex_read_epochs(fname,event,eventname,tmin,tmax)
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
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 me='MNE:mne_ex_read_epochs';
0071
0072 keep_comp = false;
0073 dest_comp = 0;
0074 pick_all = true;
0075
0076 if nargin ~= 5
0077 error(me,'Incorrect number of arguments');
0078 end
0079
0080
0081
0082 try
0083 raw = fiff_setup_read_raw(fname);
0084 catch
0085 error(me,'%s',mne_omit_first_line(lasterr));
0086 end
0087
0088 if pick_all
0089
0090
0091
0092 picks = 1:raw.info.nchan;
0093
0094 else
0095
0096
0097
0098 include{1} = 'STI 014';
0099 want_meg = true;
0100 want_eeg = false;
0101 want_stim = false;
0102
0103
0104 picks = fiff_pick_types(raw.info,want_meg,want_eeg,want_stim,include,raw.info.bads);
0105 end
0106 ch_names = raw.info.ch_names(picks);
0107
0108
0109
0110 if isempty(raw.info.projs)
0111 fprintf(1,'No projector specified for these data\n');
0112 raw.proj = [];
0113 else
0114
0115
0116
0117 for k = 1:length(raw.info.projs)
0118 raw.info.projs(k).active = true;
0119 end
0120 fprintf(1,'%d projection items activated\n',length(raw.info.projs));
0121
0122
0123
0124 [proj,nproj] = mne_make_projector_info(raw.info);
0125 if nproj == 0
0126 fprintf(1,'The projection vectors do not apply to these channels\n');
0127 raw.proj = [];
0128 else
0129 fprintf(1,'Created an SSP operator (subspace dimension = %d)\n',nproj);
0130 raw.proj = proj;
0131 end
0132 end
0133
0134
0135
0136 current_comp = mne_get_current_comp(raw.info);
0137 if current_comp > 0
0138 fprintf(1,'Current compensation grade : %d\n',current_comp);
0139 end
0140 if keep_comp
0141 dest_comp = current_comp;
0142 end
0143 if current_comp ~= dest_comp
0144 try
0145 raw.comp = mne_make_compensator(raw.info,current_comp,dest_comp);
0146 fprintf(1,'Appropriate compensator added to change to grade %d.\n',dest_comp);
0147 catch
0148 error(me,'%s',mne_omit_first_line(lasterr));
0149 end
0150 end
0151
0152
0153
0154 if isempty(eventname)
0155 p = strfind(fname,'.fif');
0156 if p > 1
0157 eventname = sprintf('%s-eve.fif',fname(1:p-1));
0158 else
0159 error(me,'Raw file name does not end properly');
0160 end
0161 events = mne_read_events(eventname);
0162 fprintf(1,'Events read from %s\n',eventname);
0163 else
0164
0165
0166
0167 p = strfind(eventname,'-eve.fif');
0168 if p > 1
0169 try
0170 events = mne_read_events(eventname);
0171 catch
0172 error(me,mne_omit_first_line(lasterr));
0173 end
0174 fprintf(1,'Binary event file %s read\n',eventname);
0175 else
0176
0177
0178
0179 try
0180 events = load(eventname);
0181 catch
0182 error(me,mne_omit_first_line(lasterr));
0183 end
0184 if size(events,1) < 1
0185 error(me,'No data in the event file');
0186 end
0187
0188
0189
0190 for p = 1:size(events,1)
0191 if events(p,1) < 0
0192 events(p,1) = events(p,2)*raw.info.sfreq;
0193 end
0194 end
0195
0196
0197
0198 events = int32(events(:,[1 3 4]));
0199
0200
0201
0202 if events(1,2) == 0 && events(1,3) == 0
0203 fprintf(1,'The text event file %s is in the new format\n',eventname);
0204 if events(1,1) ~= raw.first_samp
0205 error(me,'This new format event file is not compatible with the raw data');
0206 end
0207 else
0208 fprintf(1,'The text event file %s is in the old format\n',eventname);
0209
0210
0211
0212 events(:,1) = events(:,1) + raw.first_samp;
0213 end
0214 end
0215 end
0216
0217
0218
0219 count = 0;
0220 selected = [];
0221 for p = 1:size(events,1)
0222 if events(p,2) == 0 && events(p,3) == event
0223 count = count + 1;
0224 selected = [ selected p];
0225 end
0226 end
0227 if count > 0
0228 fprintf(1,'%d matching events found\n',count);
0229 else
0230 error(me,'No desired events found.');
0231 end
0232
0233 for p = 1:count
0234
0235
0236
0237 event_samp = events(selected(p),1);
0238 from = event_samp + tmin*raw.info.sfreq;
0239 to = event_samp + tmax*raw.info.sfreq;
0240 try
0241 if p == 1
0242 [ epoch ] = fiff_read_raw_segment(raw,from,to,picks);
0243 times = double([(int32(from)-int32(event_samp)):(int32(to)-int32(event_samp))])/raw.info.sfreq;
0244 else
0245 [ epoch ] = fiff_read_raw_segment(raw,from,to,picks);
0246 end
0247 data(p).epoch = epoch;
0248 data(p).event = event;
0249 data(p).tmin = (double(from)-double(raw.first_samp))/raw.info.sfreq;
0250 data(p).tmax = (double(to)-double(raw.first_samp))/raw.info.sfreq;
0251 catch
0252 fclose(raw.fid);
0253 error(me,'%s',mne_omit_first_line(lasterr));
0254 end
0255 end
0256 fprintf(1,'Read %d epochs, %d samples each.\n',count,length(data(1).epoch));
0257
0258 return;
0259
0260 end