0001 function [data] = fiff_read_evoked_all(fname)
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 global FIFF;
0038 if isempty(FIFF)
0039 FIFF = fiff_define_constants();
0040 end
0041
0042 me='MNE:fiff_read_evoked_all';
0043
0044 if nargin ~= 1
0045 error(me,'Incorrect number of arguments');
0046 end
0047
0048
0049
0050 fprintf(1,'Reading %s ...\n',fname);
0051 [ fid, tree ] = fiff_open(fname);
0052
0053
0054
0055 [ info, meas ] = fiff_read_meas_info(fid,tree);
0056 info.filename = fname;
0057
0058
0059
0060 processed = fiff_dir_tree_find(meas,FIFF.FIFFB_PROCESSED_DATA);
0061 if length(processed) == 0
0062 fclose(fid);
0063 error(me,'Could not find processed data');
0064 end
0065
0066 evoked = fiff_dir_tree_find(meas,FIFF.FIFFB_EVOKED);
0067 if length(evoked) == 0
0068 fclose(fid);
0069 error(me,'Could not find evoked data');
0070 end
0071
0072
0073
0074 naspect = 0;
0075 is_smsh = [];
0076 for k = 1:length(evoked)
0077 sets(k).aspects = fiff_dir_tree_find(evoked(k),FIFF.FIFFB_ASPECT);
0078 sets(k).naspect = length(sets(k).aspects);
0079 if sets(k).naspect > 0
0080 is_smsh = [ is_smsh zeros(1,sets(k).naspect) ];
0081 naspect = naspect + sets(k).naspect;
0082 end
0083 saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_SMSH_ASPECT);
0084 nsaspects = length(saspects);
0085 if nsaspects > 0
0086 sets(k).naspect = sets(k).naspect + nsaspects;
0087 sets(k).aspects = [ sets(k).aspects saspects ];
0088 is_smsh = [ is_smsh ones(1,sets(k).naspect) ];
0089 naspect = naspect + nsaspects;
0090 end
0091 end
0092 if naspect == 0
0093 fclose(fid);
0094 error(me,'No evoked data aspects found');
0095 end
0096 fprintf(1,'\t%d evoked data sets containing a total of %d data aspects in %s\n',length(evoked),naspect,fname);
0097 if ~isempty(info.comps)
0098 fprintf(1,'\t\t%d CTF compensation matrices available\n',length(info.comps));
0099 end
0100
0101
0102
0103 p = 1;
0104 for e = 1:length(evoked)
0105 if e == 1
0106
0107
0108
0109 my_evoked = evoked(e);
0110 nchan = 0;
0111 sfreq = -1;
0112 q = 0;
0113 for k = 1:my_evoked.nent
0114 kind = my_evoked.dir(k).kind;
0115 pos = my_evoked.dir(k).pos;
0116 switch kind
0117 case FIFF.FIFF_NCHAN
0118 tag = fiff_read_tag(fid,pos);
0119 nchan = tag.data;
0120 case FIFF.FIFF_SFREQ
0121 tag = fiff_read_tag(fid,pos);
0122 sfreq = tag.data;
0123 case FIFF.FIFF_CH_INFO
0124 q = q+1;
0125 tag = fiff_read_tag(fid,pos);
0126 chs(q) = tag.data;
0127 end
0128 end
0129 if nchan > 0
0130 if ~exist('chs','var')
0131 fclose(fid);
0132 error(me, ...
0133 'Local channel information was not found when it was expected.');
0134 end
0135 if length(chs) ~= nchan
0136 fclose(fid);
0137 error(me, ...
0138 'Number of channels and number of channel definitions are different');
0139 end
0140 info.chs = chs;
0141 info.nchan = nchan;
0142 fprintf(1, ...
0143 '\tFound channel information in evoked data. nchan = %d\n',nchan);
0144 if sfreq > 0
0145 info.sfreq = sfreq;
0146 end
0147 end
0148 end
0149 for a = 1:sets(e).naspect
0150 my_evoked = evoked(e);
0151 my_aspect = sets(e).aspects(a);
0152
0153
0154
0155 for k = 1:my_evoked.nent
0156 kind = my_evoked.dir(k).kind;
0157 pos = my_evoked.dir(k).pos;
0158 switch kind
0159 case FIFF.FIFF_COMMENT
0160 tag = fiff_read_tag(fid,pos);
0161 comment = tag.data;
0162 case FIFF.FIFF_FIRST_SAMPLE
0163 tag = fiff_read_tag(fid,pos);
0164 first = tag.data;
0165 case FIFF.FIFF_LAST_SAMPLE
0166 tag = fiff_read_tag(fid,pos);
0167 last = tag.data;
0168 end
0169 end
0170 if ~exist('comment','var')
0171 comment = 'No comment';
0172 end
0173 nsamp = last-first+1;
0174
0175
0176
0177 nepoch = 0;
0178 for k = 1:my_aspect.nent
0179 kind = my_aspect.dir(k).kind;
0180 pos = my_aspect.dir(k).pos;
0181 switch kind
0182 case FIFF.FIFF_COMMENT
0183 tag = fiff_read_tag(fid,pos);
0184 comment = tag.data;
0185 case FIFF.FIFF_ASPECT_KIND
0186 tag = fiff_read_tag(fid,pos);
0187 aspect_kind = tag.data;
0188 case FIFF.FIFF_NAVE
0189 tag = fiff_read_tag(fid,pos);
0190 nave = tag.data;
0191 case FIFF.FIFF_EPOCH
0192 nepoch = nepoch + 1;
0193 tag = fiff_read_tag(fid,pos);
0194 epoch(nepoch) = tag;
0195 end
0196 end
0197 if ~exist('nave','var')
0198 nave = 1;
0199 end
0200 if aspect_kind == FIFF.FIFFV_ASPECT_AVERAGE
0201 fprintf(1,'\tFound the data of interest:\n');
0202 fprintf(1,'\t\tt = %10.2f ... %10.2f ms (%s)\n',1000*first/info.sfreq,1000*last/info.sfreq,comment);
0203 fprintf(1,'\t\tnave = %d aspect type = %d\n',...
0204 nave,aspect_kind);
0205 if nepoch ~= 1 && nepoch ~= info.nchan
0206 fclose(fid);
0207 error(me,'Number of epoch tags is unreasonable (nepoch = %d nchan = %d)',nepoch,info.nchan);
0208 end
0209
0210
0211
0212 if nepoch == 1
0213 all = epoch(1).data;
0214 else
0215 all = epoch(1).data';
0216 for k = 2:nepoch
0217 all = [ all ; epoch(k).data' ];
0218 end
0219 end
0220 if size(all,2) ~= nsamp
0221 fclose(fid);
0222 error(me,'Incorrect number of samples (%d instead of %d)',size(all,2),nsamp);
0223 end
0224
0225
0226
0227 for k = 1:info.nchan
0228 cals(k) = info.chs(k).cal;
0229 end
0230 all = diag(cals)*all;
0231
0232
0233
0234 one.aspect_kind = aspect_kind;
0235 one.is_smsh = is_smsh(p);
0236 if exist('nave','var')
0237 one.nave = nave;
0238 else
0239 one.nave = 1;
0240 end
0241 one.first = first;
0242 one.last = last;
0243 if exist('comment','var')
0244 one.comment = comment;
0245 end
0246
0247
0248
0249 one.times = double(one.first:1:one.last)/info.sfreq;
0250 one.epochs = all;
0251 if p == 1
0252 data.info = info;
0253 data.evoked(1) = one;
0254 else
0255 data.evoked(p) = one;
0256 end
0257 clear('one');
0258 p = p + 1;
0259 end
0260 clear('comment');
0261 clear('nave');
0262 end
0263 end
0264
0265 fclose(fid);
0266
0267 return;
0268
0269 function [tag] = find_tag(node,findkind)
0270
0271 for p = 1:node.nent
0272 kind = node.dir(p).kind;
0273 pos = node.dir(p).pos;
0274 if kind == findkind
0275 tag = fiff_read_tag(fid,pos);
0276 return;
0277 end
0278 end
0279 tag = [];
0280 return
0281 end
0282
0283 end
0284
0285