Home > matlabmk > crw2set_contNEO.m

crw2set_contNEO

PURPOSE ^

crw2set() - Create and save to disk an EEGLAB set file from a

SYNOPSIS ^

function crw2set_contNEO(crwfile,logfile,blfORbdf_file,setfile,varargin)

DESCRIPTION ^

 crw2set() - Create and save to disk an EEGLAB set file from a
             Kutaslab crw, log, and associated files. EEGLAB must
             be started before running this function.

 Usage:

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % crw2set() - Create and save to disk an EEGLAB set file from a
0002 %             Kutaslab crw, log, and associated files. EEGLAB must
0003 %             be started before running this function.
0004 %
0005 % Usage:
0006 
0007 
0008 
0009 %%%%%%%%%%%%%%%% FUNCTION OUTLINE %%%%%%%%%%%%%%%%%
0010 % I.    IF ALT CAL PULSE CRW FILE SPECIFIED, GET THOSE CAL PULSES FIRST
0011 % II.   OPEN CRW FILE USING ERPIO() & GET EXP INFO AND RECORDING PARAMETERS
0012 % III.  IMPORT BLF FILE INFORMATION
0013 % IV.   IF ARF FILE SPECIFIED, FLAG ARTIFACTS IN LOG FILE
0014 % V.    IMPORT LOG INFO FROM ASCII VERSION OF LOGFILE
0015 % VI.   COMBINE LOG & BIN INFORMATION
0016 % VII.  LOAD INFORMATION ABOUT CLASSES OF EVENTS FROM TEXT FILE(S) [OPTIONAL]
0017 % VIII. LOAD INFORMATION ABOUT PARTICIPANT FROM TEXT FILE(S) [OPTIONAL]
0018 % IX.   LOAD INFORMATION ABOUT LOG EVENTS FROM TEXT FILE(S) [OPTIONAL]
0019 % X.    IMPORT CDBL PRODUCED REACTION TIME INFORMATION [OPTIONAL]
0020 % XI.   IMPORT EEG AND CALIBRATION PULSES
0021 % XII.  NORMALIZE CAL PULSES AND DATA
0022 % XIII. CREATE EEGLAB SET FILE AND SAVE TO DISK
0023 % XIV.  RE-REFERENCE TO BIMASTOID REF [OPTIONAL]
0024 % XV.   REMOVE BASELINE MEAN FROM EEG [OPTIONAL]
0025 % XVI.  SAVE SET AND CLEANUP
0026 
0027 % NOTES
0028 %  erpio('getcdata', 'rawfile', 'event'|'msec', start, stop, padding)
0029 %         This opens the specified rawfile and reads in continuous
0030 %         data from start to stop.  If the third parameter is the string
0031 %         "event", start and stop are taken as ordinal event numbers
0032 %         in the raw file (as shown by "item number" by the logexam
0033 %         program - note they start at 0); if it is "msec", start and
0034 %         stop are taken as integer numbers of milliseconds from the
0035 %         beginning of the raw file.  A start of 0 is considered the
0036 %         beginning of the raw file; a stop of 0 is considered the end
0037 %         of the raw file.  'padding' specifies to include an additional
0038 %         number of msec of data on either side of the boundaries (0 if
0039 %         none is desired).  The return value is an MxN matrix containing
0040 %         the continuous section of data, where M is the number of
0041 %         channels (including an additional channel containing event
0042 %         codes as the first channel) and N is the number of sample
0043 %         points in the continuous section of data.  Note the continuous
0044 %         section of data is aligned on the nearest raw record boundaries
0045 %         that include the start and stop boundaries, so the amount of
0046 %         data returned may be slightly more than specified.
0047 
0048 
0049 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%%
0050 %
0051 %
0052 
0053 % POSSIBLE FUTURE DEVELOPMENT
0054 %
0055 
0056 % TO DO ??
0057 % Make it possible to use a different crw file to calibrate
0058 % Make it possible to combine multiple crw files into a single file
0059 % Double check optional inputs (make sure they're used) and update comments
0060 
0061 function crw2set_contNEO(crwfile,logfile,blfORbdf_file,setfile,varargin)
0062 %??
0063 
0064 global VERBLEVEL
0065 global SUBJECT
0066 
0067 %EEGLAB global variables:
0068 global EEG
0069 global ALLEEG
0070 
0071 %if 1, %REMOVE ?? PUT BACK
0072 %Input Parser
0073 p=inputParser;
0074 %Note: the order of the required arguments needs to match precisely their
0075 %order in the function definition (which is also the order used by p.parse
0076 %below)
0077 
0078 % limits,limit_types, (make these optional)
0079 p.addRequired('crwfile',@ischar);
0080 p.addRequired('logfile',@ischar);
0081 p.addRequired('blfORbdf_file',@ischar);
0082 p.addRequired('setfile',@ischar);
0083 p.addParamValue('locfile',[],@ischar); %made optional
0084 p.addParamValue('presam',100,@(x) x>0);
0085 p.addParamValue('cprecis',1,@(x) x>0 & mod(x,1)==0);
0086 p.addParamValue('decimat',1,@(x) x>0 & mod(x,1)==0);
0087 p.addParamValue('verblevel',2,@(x) x>=0);
0088 p.addParamValue('rtfile',[],@ischar);
0089 p.addParamValue('event_list_file',[],@ischar);
0090 p.addParamValue('limits',[0 0],@(x) isnumeric(x) && length(x)==2); % new ??
0091 p.addParamValue('limits_type','msec',@(x) strcmpi(x,'msec') || strcmpi(x,'event')); %new ??
0092 p.addParamValue('padding',0,@(x) isnumeric(x) && length(x)==1 && x>=0); %NEW in units of ms
0093 p.addParamValue('event_infofile',[],@(x) ischar(x) | iscell(x));
0094 p.addParamValue('code_infofile',[],@(x) ischar(x) | iscell(x));
0095 p.addParamValue('sbjct_infofile',[],@(x) ischar(x) | iscell(x));
0096 p.addParamValue('sbjct_id',[],@(x) ischar(x) | isnumeric(x));
0097 p.addParamValue('cal_plot_opt',1,@(x) x==0 | x==1); %plot cal pulses
0098 p.addParamValue('cal_plot_avg',0,@(x) x==0 | x==1); %plot cal pulse avg (time intensive)
0099 p.addParamValue('cal_npts',10,@(x) x>0 & mod(x,1)==0); %number of time
0100 p.addParamValue('block_plot_opt',1,@(x) x==0 | x==1); %plot sweeps with amplifier blocking
0101 %points to include in cal window
0102 p.addParamValue('low_cursor',-50,@(x) x<0); %center of pre-pulse
0103 %baseline window (in msec)
0104 p.addParamValue('hi_cursor',50,@(x) x>0); %center of post-pulse
0105 %baseline window (in msec)
0106 p.addParamValue('cal_amp',10,@isnumeric); %cal pulse amplitude
0107 %(in microvolts)
0108 p.addParamValue('cal_polarity',1,@(x) x==1 | x==-1); %cal pulse polarity
0109 p.addParamValue('trm_pcnt',5,@(x) x>=0 & x<100);
0110 p.addParamValue('reref_bimast','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0111 p.addParamValue('rm_blocking','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0112 p.addParamValue('reref_ignore',[],@(x) ischar(x) | iscell(x));
0113 p.addParamValue('exclude_chans',[],@(x) ischar(x) | iscell(x));
0114 p.addParamValue('include_chans',[],@(x) ischar(x) | iscell(x));
0115 %remove mean EEG in given time window (in msec) from each epoch
0116 p.addParamValue('rm_bsln_mn',[],@(x) isnumeric(x) & (length(x)==2));
0117 p.addParamValue('cal_crwfile',[],@ischar);
0118 p.addParamValue('cal_logfile',[],@ischar);
0119 p.addParamValue('cal_bdf_file',[],@ischar);
0120 
0121 p.parse(crwfile,logfile,blfORbdf_file,setfile,varargin{:});
0122 
0123 VERBLEVEL=p.Results.verblevel;
0124 
0125 %Show settings of all arguments
0126 if VERBLEVEL>0
0127     fprintf('crw2set argument values:\n');
0128     disp(p.Results);
0129 end
0130 
0131 
0132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0133 % I. IMPORT LOG INFO FROM ASCII VERSION OF LOGFILE
0134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0135 VerbReport(' ',1,VERBLEVEL);
0136 % If ascii version of logfile not specified or if new log file created with artifact flags,
0137 % create temporary ascii file
0138 
0139 % Misc info that needs to be read from crw file
0140 srate=erpio2('get_srate',crwfile);
0141 sub_codename=erpio2('get_subdes',crwfile);
0142 
0143 VerbReport(sprintf('Creating temporary ascii version of logfile %s',logfile),1,VERBLEVEL);
0144 %remove temp.asci if it exists
0145 asci_cmnd='rm -f temp.asci';
0146 unix(asci_cmnd);
0147 asci_cmnd=sprintf('log2asci %s temp.asci -d %d',logfile,srate);
0148 [s, w]=unix(asci_cmnd);
0149 if s~=0,
0150     disp('Could not create ascii version of logfile using log2asci.');
0151     error('According to log2asci: %s',w);
0152 end
0153 asciilogfile='temp.asci';
0154 tempascii=1;
0155 VerbReport(sprintf('Getting log information from %s', asciilogfile), 1, VERBLEVEL);
0156 
0157 %import log information for events caught by blf file
0158 log_info=import_entire_log(asciilogfile);
0159 
0160 %If a temp ascii version of logfile was created, delete it
0161 if  tempascii,
0162     VerbReport(sprintf('Removing temporary ascii version of logfile.'),1,VERBLEVEL);
0163     asci_cmnd='rm -f temp.asci';
0164     [s, w]=unix(asci_cmnd);
0165     if s~=0,
0166         disp('Could not remove ascii version of logfile: temp.asci.');
0167         error('According to rm: %s',w);
0168     end
0169 end
0170 
0171 % Build epoch information descriptions for later ...
0172 log_LongDesc = {'Log Item Number', 'Event Code', 'Condition Code', 'Log Flag (40=nonblink art; 60=blink art)','TIME'};
0173 
0174 % Parse log file up by condition code
0175 [cc_bounds, cc_bounds_codes]=parse_log_by_cc(log_info);
0176 
0177 
0178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0179 % II. IMPORT BLF/BDF FILE INFORMATION
0180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0181 [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]= ...
0182     import_blf(blfORbdf_file,logfile,srate,p.Results.rtfile);
0183 num_bins=size(blf_info,2);
0184 % Each row of blf_info corresponds to an event, the 1's in the row indicate
0185 % which bin the event belongs to
0186 
0187 
0188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0189 % III. IMPORT EEG DATA FROM CRW FILE
0190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0191 
0192 if isequal(p.Results.limits,[0 0])
0193     %No limits specified by user; Default behavior
0194     cont_data=[];
0195     eventcodes=[];
0196     timestamps=[];
0197     used_blf_info=[];
0198     used_blf_evnum=[];
0199     n_cc_bounds=size(cc_bounds,1);
0200     for a=1:n_cc_bounds,
0201         if cc_bounds_codes(a)
0202             % Only import data corresponding to non-zero condition codes
0203             [cont_dataTEMP, eventcodesTEMP, timestampsTEMP]=erpio2('get_cdata', ...
0204                 crwfile,'event',cc_bounds(a,1)-1,cc_bounds(a,2)-1, ...
0205                 p.Results.padding); %note you have to subtract 1 because logfile event indices start at 0
0206             if isempty(cont_dataTEMP),
0207                 error(erpio2('get_errstr'));
0208             end
0209             
0210             blf_ids=find( (blf_evnum>=(cc_bounds(a,1)-1)).*(blf_evnum<=(cc_bounds(a,2)-1)) );
0211             %note you have to subtract 1 from cc_bounds because logfile event indices
0212             %start at 0
0213             used_blf_info=[used_blf_info; blf_info(blf_ids,:)];
0214             used_blf_evnum=[used_blf_evnum; blf_evnum(blf_ids)];
0215             cont_data=[cont_data cont_dataTEMP];
0216             eventcodes=[eventcodes eventcodesTEMP];
0217             timestamps=[timestamps timestampsTEMP];
0218         end
0219     end
0220 else
0221     [cont_data, eventcodes, timestamps]=erpio2('get_cdata', ...
0222         crwfile,p.Results.limits_type,p.Results.limits(1),p.Results.limits(2), ...
0223         p.Results.padding);
0224     if isempty(cont_data),
0225         error(erpio2('get_errstr'));
0226     end
0227     used_blf_info=blf_info;
0228     used_blf_evnum=blf_evnum;
0229 end
0230 clear cont_dataTEMP eventcodesTEMP timestampsTEMP
0231 [n_chan, n_pnt]=size(cont_data);
0232 
0233 
0234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0235 % II. TAKE CARE OF CHANNEL LOCATION INFORMATION
0236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0237 
0238 %convert p.Results.exclude_chans/include_chans to a cell array if it is not already
0239 if isempty(p.Results.exclude_chans),
0240     exclude_chans=[];
0241 elseif iscell(p.Results.exclude_chans),
0242     exclude_chans=p.Results.exclude_chans;
0243 else
0244     exclude_chans{1}=p.Results.exclude_chans;
0245 end
0246 if isempty(p.Results.include_chans),
0247     include_chans=[];
0248 elseif iscell(p.Results.include_chans),
0249     include_chans=p.Results.include_chans;
0250 else
0251     include_chans{1}=p.Results.include_chans;
0252 end
0253 if isempty(p.Results.locfile)
0254     %attempt to create EEGLAB chanlocs struct from channel names
0255     use_chanlocs=chanlocs_from_crw_erpio2(crwfile,exclude_chans,include_chans);
0256 else
0257     use_chanlocs=p.Results.locfile;
0258     % Get channel names from crw file
0259     n_chans=erpio2('get_nchans',crwfile);
0260     errstring=erpio2('get_errstr'); % check for error
0261     if ~isempty(errstring),
0262         error(errstring);
0263     end
0264     
0265     crw_chanlabels=cell(1,n_chans);
0266     excluded_chan_ct=0;
0267     use_chans=zeros(1,n_chans); %keep track of which channels should be included
0268     for c=1:n_chans,
0269         chan_label=erpio2('get_chandes',crwfile,c-1); %have to subtract 1 because channel indexing starts at 0
0270         errstring=erpio2('get_errstr'); % check for error
0271         if ~isempty(errstring),
0272             error(errstring);
0273         end
0274         crw_chanlabels{c}=chan_label;
0275         use_chans(c)=1; %assume the channel will be included by default
0276         if ~isempty(exclude_chans) && ismember(chan_label,exclude_chans),
0277             excluded_chan_ct=excluded_chan_ct+1;
0278             excluded_chans{excluded_chan_ct}=chan_label;
0279             use_chans(c)=0;
0280         end
0281         if ~isempty(include_chans) && ~ismember(chan_label,include_chans),
0282             use_chans(c)=0;
0283         end
0284     end
0285     use_chan_labels=crw_chanlabels(find(use_chans));
0286    
0287     if ~isempty(exclude_chans)
0288         % double check that all excluded chans were excluded
0289         missed_chans=setdiff(exclude_chans,excluded_chans);
0290         if ~isempty(missed_chans),
0291             msg=['I could not find the following channels in your crw file and thus cannot exclude them:'];
0292             for a=1:length(missed_chans),
0293                 msg=[msg ' ' missed_Chans{a}];
0294             end
0295             error(msg);
0296         end
0297     elseif ~isempty(include_chans)
0298         % double check that all included chans were included
0299         missed_chans=setdiff(include_chans,use_chan_labels);
0300         if ~isempty(missed_chans),
0301             msg=['I could not find the following channels in your crw file:'];
0302             for a=1:length(missed_chans),
0303                 msg=[msg ' ' missed_Chans{a}];
0304             end
0305             error(msg);
0306         end
0307     end
0308     
0309     % Double check that used chans are compatible with loc file
0310     fid = fopen(use_chanlocs);
0311     locfile_stuff = textscan(fid,'%s%s%s%s');
0312     fclose(fid);
0313     locfile_channames=locfile_stuff{4};
0314     
0315     if length(locfile_channames)~=length(use_chan_labels),
0316         error('Number of channels in file %s differ from the number requested from %s.', ...
0317             use_chanlocs,crwfile);
0318     end
0319     
0320     for a=1:length(locfile_channames),
0321         % Go through locfile chan names and removing trailing periods
0322         % Trailiing periods are an old EEGLAB convention from when all channel
0323         % names had to have the same number of characters in a loc file
0324         locfile_channames{a}=locfile_channames{a}(find(locfile_channames{a}~='.'));
0325         
0326         %Also make sure locfile channels agree with requested crw file
0327         %channels
0328         if ~strcmpi(locfile_channames{a},use_chan_labels{a})
0329            error('Channel #%d in file %s is %s, but requested Channel #%d from %s is %s.', ...
0330                a,use_chanlocs,locfile_channames{a},a,crwfile,use_chan_labels{a});
0331         end
0332     end
0333 end
0334 
0335 %Remove data from undesired channels (if any)
0336 cont_data=cont_data(find(use_chans),:);
0337 
0338 %end %REMOVE ?? PUT BACK
0339 %%
0340 
0341 
0342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0343 % IV??. CREATE BASIC EEG VARIABLE
0344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0345 % unused pop_importdata options
0346 % 'condition'  - [string] task condition. For example, 'Targets'
0347 %                    {default: none -> all datasets from one condition}
0348 %    'group'      - [string] subject group. For example 'Patients' or 'Control'.
0349 %                    {default: none -> all subjects in one group}
0350 %    'session'    - [integer] session number (from the same subject). All datasets
0351 %                    from the same subject and session will be assumed to use the
0352 %                    same ICA decomposition {default: none -> each dataset from
0353 %                    a different session}
0354 %     'ref'        - [string or integer] reference channel indices. 'averef' indicates
0355 %                    average reference. Note that this does not perform referencing
0356 %                    but only sets the initial reference when the data is imported.
0357 %     'icaweight'  - [matrix] ICA weight matrix.
0358 %     'icasphere'  - [matrix] ICA sphere matrix. By default, the sphere matrix
0359 %                    is initialized to the identity matrix if it is left empty.
0360 %     'comments'   - [string] Comments on the dataset, accessible through the EEGLAB
0361 %                    main menu using (Edit > About This Dataset). Use this to attach
0362 %                    background information about the experiment or data to the dataset.
0363 % Perhaps these would be of use someday?
0364 
0365 %cont_data=[cont_data; eventcodes];
0366 [setfilepath, setfilename]=pathNname(setfile);
0367 EEG = pop_importdata('dataformat','array','data',cont_data, ....
0368     'setname',setfilename(1:(length(setfilename)-4)),'srate',srate,'subject',sub_codename, ...
0369     'chanlocs',use_chanlocs,'pnts',n_pnt,'nbchan',n_chan,'xmin',0);
0370 clear cont_data; %save memory
0371 % Note, first time point is also 0 in Kutaslab logfiles
0372 % Also note that pop_importdata will throw some warnings as it
0373 % figures out how to set some parameters that can't (apparently) be set via
0374 % input arguments
0375 EEG = eeg_checkset( EEG );
0376 
0377 % Should I be using these?
0378 %     'subject', SUBJECT.PARAMS.subdesc, ...
0379 %     'nbchan', nchans, ...
0380 %     'comments', SUBJECT.PARAMS.expdesc, ...
0381 
0382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0383 % IV??. IMPORT EVENT INFORMATION
0384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0385 %%  Write temporary text file of event information.  There's got to be a
0386 % way of doing this strictly within MATLAB, but this was the best I could
0387 % do without going into EEGLAB source code.
0388 if isempty(p.Results.event_list_file),
0389     %make text file of event information
0390     VerbReport(sprintf('Creating temporary textfile of event information (e.g., the latency and event code of each event): temp_event_info.txt'),1,VERBLEVEL);
0391     [fid, fopen_msg]=fopen('temp_event_info.txt','w');
0392     if fid==-1,
0393         disp('Could not create temporary event information file.');
0394         error('According to fopen: %s',fopen_msg);
0395     end
0396     
0397     % First add events from the "Event #" column of the logfile along with
0398     % the corresponding time (in msec) from the "Time" column
0399     fprintf(fid,'latency type conditioncode\n');
0400     event_ids=find(eventcodes~=0);
0401     for a=event_ids,
0402         log_info_id=find(log_info(:,5)==timestamps(a)/1000); %log_info is in units of seconds but timestamps in units of ms
0403         if isempty(log_info_id),
0404            error('Can''t find condition code for Event #%d, Time %g ms.\n', ...
0405                eventcodes(a),timestamps(a));
0406         end
0407         ccode=log_info(log_info_id,3);
0408         fprintf(fid,'%d %d %d\n',timestamps(a),eventcodes(a),ccode); %note time stamps is in msec and eventcodes should be an array of integers
0409     end
0410     fclose(fid);
0411     
0412     %Add event information to EEG variable
0413     EEG = pop_importevent(EEG, 'event', ...
0414         'temp_event_info.txt', 'fields', {'latency','type','conditioncode'}, ...
0415         'skipline',1,'timeunit',0.001 );
0416 
0417     if 0, %fix later ??
0418         %Remove temporary text file
0419         VerbReport(sprintf('Removing temporary textfile of event information: temp_event_info.txt'),1,VERBLEVEL);
0420         asci_cmnd='rm -f temp_event_info.txt';
0421         [s, unix_msg]=unix(asci_cmnd);
0422         if s~=0,
0423             disp('Could not remove temporary textfile of event information: temp_event_info.txt');
0424             error('According to rm: %s',unix_msg);
0425         end
0426     end
0427 else
0428     %Do the same thing as above but with specified text file
0429     %Add event information to EEG variable
0430     EEG = pop_importevent(EEG, 'event', ...
0431         p.Results.event_list_file, 'fields', {'latency','type'}, ...
0432         'skipline',1,'timeunit',0.001 );
0433 end
0434 n_EEG_ev=length(EEG.event);
0435 
0436 %% Now add "bin#" to events based on the blf file
0437 %First get latencies from all EEG variable events
0438 EEG_ev_latencies=zeros(1,n_EEG_ev);
0439 for a=1:n_EEG_ev,
0440    EEG_ev_latencies(a)=EEG.event(a).latency;
0441 end
0442 
0443 n_blf=length(used_blf_evnum);
0444 n_log_info=size(log_info,1);
0445 for a=1:n_blf,
0446     % Get logfile index of this event
0447     log_id=used_blf_evnum(a)+1;
0448 
0449     if log_id>n_log_info,
0450        error('blf file thinks there should be at least % events in the log file.  However, there are only %d.\n', ... 
0451            log_id,n_log_info);
0452     end
0453     
0454     % Get latency of event in timepoints
0455     blf_evnum_lat=round(log_info(log_id,5)*EEG.srate)+1; %have to add one since first time point is 0 sec
0456     EEG_event_id=find(round(EEG_ev_latencies)==blf_evnum_lat);
0457     if isempty(EEG_event_id),
0458         %Try one time point earlier
0459         EEG_event_id=find(round(EEG_ev_latencies)==(blf_evnum_lat-1));
0460         if isempty(EEG_event_id),            
0461             %Try one time point later
0462             EEG_event_id=find(round(EEG_ev_latencies)==(blf_evnum_lat+1));
0463             if isempty(EEG_event_id),
0464                 error('Could not find an entry in EEG.event with the same latency as Item #%d in the blf file.', ...
0465                     a);
0466             else
0467                 watchit(sprintf('Could not find an exact latency match for Item #%d in the blf file, but I''m using a match that''s one time point later.', ...
0468                     a));
0469             end
0470         else
0471             watchit(sprintf('Could not find an exact latency match for Item #%d in the blf file, but I''m using a match that''s one time point earlier.', ...
0472                 a));
0473         end
0474     end
0475     
0476     %Add bin information to EEG.event
0477     EEG.event(EEG_event_id).bins=find(used_blf_info(a,:))-1;
0478 end
0479 
0480 %% Report number of trials in each bin FIX??
0481 bin_ct=zeros(1,num_bins);
0482 for a=1:n_EEG_ev,
0483     bins=EEG.event(a).bins;
0484     if ~isempty(bins)
0485         bin_ct(bins+1)=bin_ct(bins+1)+1; %have to add 1 to bins because there is a Bin 0
0486     end
0487 end
0488 
0489 %%
0490 fprintf('The following number of trials were found for each bin.\n');
0491 for a=1:num_bins,
0492     if (a-1)==cal_bin,
0493         fprintf('Bin #%d (Cal Pulses): %d\n',a-1,sum(used_blf_info(:,a)));
0494     else
0495         if (a-1)>cal_bin,
0496             fprintf('Bin #%d [%s]: %d\n',a-1,bin_LongDesc{a-1},sum(used_blf_info(:,a)));
0497         else
0498             fprintf('Bin #%d [%s]: %d\n',a-1,bin_LongDesc{a},sum(used_blf_info(:,a)));
0499         end
0500     end
0501 end
0502 
0503 %% Find all events of type -16384 and turn them into 'boundary' events. This
0504 % event is pause on dig machines.  'boundary' is a special type of event in
0505 % EEGLAB.
0506 boundary_ct=0;
0507 for a=1:n_EEG_ev,
0508     boundary_flag=0;
0509     if ischar(EEG.event(a).type)
0510         if strcmpi(EEG.event(a).type,'-16384')
0511             boundary_flag=1;
0512         end
0513     elseif EEG.event(a).type==-16384
0514         boundary_flag=1;
0515     end
0516     
0517     if boundary_flag
0518         EEG.event(a).type='boundary';
0519         boundary_ct=boundary_ct+1;
0520     end
0521 end
0522 VerbReport(sprintf('Number of boundaries (e.g., pauses of dig machine) detected in imported continuous data: %d\n',boundary_ct), ...
0523     2,VERBLEVEL);
0524 
0525 %add Kutaslab  bin and condition code fields
0526 EEG.bindesc=bin_LongDesc;
0527 EEG.binccodes=bin_CondCode;
0528 EEG.condesc=cond_LongDesc;
0529 EEG.crw2set_params=p.Results(1); %store arguments to crw2set with the set file it creates
0530 
0531 
0532 %%
0533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0534 % IV??. CALIBRATE DATA
0535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0536 % It's easier to do this after EEG variable has been created, since it's a
0537 % bit more direct to compare channel info in EEG variable and file
0538 
0539 % params.cal_npts=p.Results.cal_npts; ??
0540 % params.low_cursor:   center of pre-pulse baseline window (in msec)
0541 % params.hi_cursor:    center of post-pulse baseline window (in msec)
0542 % params.cal_amp:      cal pulse amplitude (in uV)
0543 % params.cal_polarity: cal pulse polarity
0544 % params.trm_pcnt:     percentage of most extreme cal pulses to discard (used when
0545 % params.cal_plot_opt: If non-zero, a figure with calibration diagnostic
0546 % params.cal_plot_avg: [1 or 0] If non-zero and cal_plot_op is non-zero,
0547 
0548 
0549 if isempty(p.Results.cal_crwfile)
0550     % Use cal pulses in the crw file whose data we're reading
0551     % Note, function assumes that any event captured by the bdf file with a
0552     % condition code of 0 is a cal pulse
0553     [scale_fact, used_cal_ids]=cont_data_calibrate(crwfile,logfile, ...
0554         blfORbdf_file,EEG.chanlocs,p.Results);
0555 else
0556     %alt crwfile
0557     [scale_fact, used_cal_ids]=cont_data_calibrate(p.Results.cal_crwfile, ...
0558         p.Results.cal_logfile,p.Results.cal_bdf_file,EEG.chanlocs,p.Results);
0559 end
0560 EEG.data=EEG.data.*repmat(scale_fact,1,n_pnt);
0561 
0562 if 0,
0563     %?? add cal pulse fields?
0564     EEG.cal_info.erps=cal_erps;
0565     EEG.cal_info.usedcal_itemnum=usedcal_itemnum;
0566     EEG.cal_info.npulse_psbl=npulse_psbl;
0567     EEG.cal_info.npulse_used=npulse_used;
0568 end
0569 
0570 
0571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0572 % XIV??. RE-REFERENCE TO BIMASTOID REF [OPTIONAL]
0573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0574 if strcmpi(p.Results.reref_bimast,'yes')
0575     %find mastoid electrode
0576     mastoid_id=[];
0577     VerbReport(' ',1,VERBLEVEL);
0578     VerbReport('Looking for channel labeled A1 or A2.',2,VERBLEVEL);
0579     for dg=1:length(EEG.chanlocs),
0580         if strcmpi(EEG.chanlocs(dg).labels,'A2') || strcmpi(EEG.chanlocs(dg).labels,'A1')
0581             mastoid_id=dg;
0582             VerbReport(sprintf('Found mastoid electrode: Channel %d, %s', ...
0583                 dg,EEG.chanlocs(dg).labels), 1, VERBLEVEL);
0584             break;
0585         end
0586     end
0587     if isempty(mastoid_id)
0588         watchit(sprintf(['Could not find mastoid electrode labeled "A1"' ...
0589             ' or "A2".\nRe-reference aborted.  Data were' ...
0590             ' not modified.']));
0591     else
0592         %find any electrodes that shouldn't be re-referenced (e.g., horizontal
0593         % eye montage)
0594         if isempty(p.Results.reref_ignore)
0595             ignore=[];
0596             VerbReport(['I assume all electrodes were' ...
0597                 ' recorded referenced to the other mastoid.'],1,VERBLEVEL);
0598         else
0599             if ischar(p.Results.reref_ignore)
0600                 ignore{1}=p.Results.reref_ignore;
0601             else
0602                 ignore=p.Results.reref_ignore;
0603             end
0604             VerbReport(sprintf(['I assume all electrodes were' ...
0605                 ' recorded referenced to the other mastoid save for the' ... 
0606                 ' %d that I was told to ignore (via the "reref_ignore" option).'],length(ignore)),1,VERBLEVEL);
0607         end
0608         reref_bimastoidEEG(mastoid_id,ignore);
0609     end
0610     VerbReport(' ',1,VERBLEVEL);
0611 end
0612 
0613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0614 % XVI??. SAVE SET AND CLEANUP
0615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0616 EEG=eeg_checkset(EEG);
0617 [ALLEEG EEG ALLEEGid] = eeg_store(ALLEEG, EEG, 1);
0618 %Note, use eeg_store without CURRENTSET
0619 %to keep adding new data sets into memory without deleting already
0620 %loaded sets [ALLEEG EEG ALLEEGid] = eeg_store(ALLEEG, EEG, CURRENTSET);
0621 
0622 EEG = pop_saveset( EEG,  'filename',  setfilename, 'filepath', setfilepath);
0623 
0624 %call eeg_store one more time to set:
0625 %ALLEEG(ALLEEGid).filename=setfilename;
0626 %ALLEEG(ALLEEGid).filepath=setfilepath;
0627 [ALLEEG EEG] = eeg_store(ALLEEG, EEG, ALLEEGid);
0628 %eeglab redraw;
0629 
0630 %CLEAN-UP
0631 clear global SUBJECT
0632 %remove temp mkp files in case they exist
0633 if strcmpi(logfile,'temp.log'),
0634     VerbReport('Removing temporary logfile: temp.log', 2, VERBLEVEL);
0635     unix('rm -f temp.log');
0636 end
0637 if strcmpi(blffile,'temp.blf'),
0638     VerbReport('Removing temporary files: temp.rt, temp.blf', 2, VERBLEVEL);
0639     unix('rm -f temp.blf');
0640     unix('rm -f temp.rt');
0641 end
0642 
0643 %fix comments ??
0644 
0645 disp('HERE!!');

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005