Home > vbmeg > external > mne > mne_ex_read_epochs.m

mne_ex_read_epochs

PURPOSE ^

SYNOPSIS ^

function [data,times,ch_names] = mne_ex_read_epochs(fname,event,eventname,tmin,tmax)

DESCRIPTION ^

   Example of reading raw data

   [ data, times, ch_names ] = mne_ex_read_epochs(fname,event,eventname,tmin,tmax)

   Input :

   fname       - The name of the input file
   event       - The event
   eventname   - Name of the event file
   tmin        - Starting time in seconds
   tmax        - Ending time in seconds

   Output :

   data        - Array of structures corresponding to the epochs with fields:

                 epoch    the epoch, channel by channel
                 event    event #
                 tmin     starting time in the raw data file (initial skip omitted)
                 tmax     ending stime in the raw data file (initial skip omitted)

   times       - The time points of the samples, in seconds
   ch_names    - Names of the channels included


   NOTE 1: The purpose of this function is to demonstrate the raw data reading
   routines. You may need to modify this for your purposes

   NOTE 2: You need to run mne_process_raw once as

   mne_process_raw --raw <fname> --projoff

   to create the fif-format event file (or open the file in mne_browse_raw).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data,times,ch_names] = mne_ex_read_epochs(fname,event,eventname,tmin,tmax)
0002 %
0003 %   Example of reading raw data
0004 %
0005 %   [ data, times, ch_names ] = mne_ex_read_epochs(fname,event,eventname,tmin,tmax)
0006 %
0007 %   Input :
0008 %
0009 %   fname       - The name of the input file
0010 %   event       - The event
0011 %   eventname   - Name of the event file
0012 %   tmin        - Starting time in seconds
0013 %   tmax        - Ending time in seconds
0014 %
0015 %   Output :
0016 %
0017 %   data        - Array of structures corresponding to the epochs with fields:
0018 %
0019 %                 epoch    the epoch, channel by channel
0020 %                 event    event #
0021 %                 tmin     starting time in the raw data file (initial skip omitted)
0022 %                 tmax     ending stime in the raw data file (initial skip omitted)
0023 %
0024 %   times       - The time points of the samples, in seconds
0025 %   ch_names    - Names of the channels included
0026 %
0027 %
0028 %   NOTE 1: The purpose of this function is to demonstrate the raw data reading
0029 %   routines. You may need to modify this for your purposes
0030 %
0031 %   NOTE 2: You need to run mne_process_raw once as
0032 %
0033 %   mne_process_raw --raw <fname> --projoff
0034 %
0035 %   to create the fif-format event file (or open the file in mne_browse_raw).
0036 %
0037 
0038 %
0039 %   Author : Matti Hamalainen, MGH Martinos Center
0040 %   License : BSD 3-clause
0041 %
0042 %   Revision 1.7  2009/03/21 00:17:35  msh
0043 %   Added one missing semicolon.
0044 %
0045 %   Revision 1.6  2009/03/12 17:59:06  msh
0046 %   Included channel names were not picked.
0047 %
0048 %   Revision 1.5  2009/01/27 22:02:17  msh
0049 %   Added some more diagnostic output
0050 %
0051 %   Revision 1.4  2009/01/27 21:55:44  msh
0052 %   Fixed error in reading a text event file with negative sample numbers.
0053 %   The conversion from time to event numbers was not correctly done.
0054 %
0055 %   Revision 1.3  2009/01/12 20:30:16  msh
0056 %   Improved the comments
0057 %
0058 %   Revision 1.2  2009/01/10 18:21:26  msh
0059 %   Added an option to specify the event file
0060 %   Changed the format of the output data
0061 %
0062 %   Revision 1.1  2008/10/10 16:13:57  msh
0063 %   Added mne_ex_read_epochs. Fixed help text of mne_write_cov_file.m
0064 %
0065 %
0066 
0067 %
0068 %   Fiddle with the arguments
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 %   Setup for reading the raw data
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     % Pick all
0091     %
0092     picks = 1:raw.info.nchan;
0093     %
0094 else
0095     %
0096     %   Set up pick list: MEG + STI 014 - bad channels (modify to your needs)
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 %   Set up projection
0109 %
0110 if isempty(raw.info.projs)
0111     fprintf(1,'No projector specified for these data\n');
0112     raw.proj = [];
0113 else
0114     %
0115     %   Activate the projection items
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     %   Create the projector
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 %   Set up the CTF compensator
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 %  Read the events
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     %   Binary file
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         %   Text file
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         %   Convert time to samples if sample number is negative
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         %    Select the columns of interest (convert to integers)
0197         %
0198         events = int32(events(:,[1 3 4]));
0199         %
0200         %    New format?
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             %   Offset with first sample
0211             %
0212             events(:,1) = events(:,1) + raw.first_samp;
0213         end
0214     end
0215 end
0216 %
0217 %    Select the desired events
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     %       Read a data segment
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005