0001 function mne_make_combined_event_file(rawname,eventname,include,all,threshold)
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 me='MNE:mne_make_combined_event_file';
0042
0043
0044
0045 try
0046 raw = fiff_setup_read_raw(rawname);
0047 catch
0048 error(me,'%s',mne_omit_first_line(lasterr));
0049 end
0050
0051
0052
0053 if nargin < 3 || isempty(include)
0054 include{1} = 'STI 001';
0055 include{2} = 'STI 002';
0056 include{3} = 'STI 003';
0057 include{4} = 'STI 004';
0058 include{5} = 'STI 005';
0059 include{6} = 'STI 006';
0060 end
0061 if nargin < 4
0062 all = false;
0063 end
0064 if nargin < 5
0065 threshold = 0.1;
0066 end
0067 want_meg = false;
0068 want_eeg = false;
0069 want_stim = false;
0070
0071 picks = fiff_pick_types(raw.info,want_meg,want_eeg,want_stim,include,raw.info.bads);
0072
0073 from = raw.first_samp;
0074 to = raw.last_samp;
0075
0076
0077
0078
0079 try
0080 [ data, times ] = fiff_read_raw_segment(raw,from,to,picks);
0081 catch
0082 fclose(raw.fid);
0083 error(me,'%s',mne_omit_first_line(lasterr));
0084 end
0085 fprintf(1,'Read %d samples.\n',size(data,2));
0086
0087
0088
0089 fclose(raw.fid);
0090
0091
0092
0093 samples=[from:to];
0094 comb=zeros(1,size(data,2));
0095 for j = 1:size(data,1)
0096 for k = 1:size(data,2)
0097 if data(j,k) > threshold
0098 data(j,k) = 1;
0099 comb(k) = comb(k) + 2^(j-1);
0100 end
0101 end
0102 end
0103
0104
0105
0106 if exist('eventname','var') && ~isempty(eventname)
0107 fd = fopen(eventname,'w');
0108 if fd < 0
0109 error(me,'Cannot open file %s',eventname);
0110 end
0111 else
0112 fd = -1;
0113 end
0114
0115 p = 0;
0116 q = 0;
0117 for k = 2:size(data,2)
0118 if comb(k) ~= comb(k-1)
0119 if fd >= 0 && (all || comb(k-1) == 0)
0120 fprintf(fd,'%6d %-10.4f %3d %3d\n',samples(k)-samples(1),times(k)-times(1),comb(k-1),comb(k));
0121 q = q + 1;
0122 end
0123 p = p + 1;
0124 events(p,1:3) = [ samples(k) comb(k-1) comb(k) ];
0125 end
0126 end
0127 if fd >= 0
0128 fprintf(1,'Wrote text event file %s (%d events)\n',eventname,q);
0129 fclose(fd);
0130 end
0131
0132
0133
0134 eventname_fif = strrep(rawname,'.fif','-eve.fif');
0135 try
0136 mne_write_events(eventname_fif,events);
0137 catch
0138 error(me,'%s',mne_omit_first_line(lasterr));
0139 end
0140 fprintf(1,'Wrote binary event file %s (%d events)\n',eventname_fif,size(events,1));
0141
0142 return;
0143
0144 end