import_blf() - Reads a blf or bdf file into Matlab for use by crw2set.m. Usage: >> [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, bin_CondCode]=import_blf(blfORbdf_file,logfile,srate,rtfile_arg) Required Global Variable: VERBLEVEL - matlabMK level of verbosity (i.e., tells functions how much to report about what they're doing during runtime) Inputs: blfORbdf_file - [string] Filename of blf or bdf file (include path unless file is in current working directory). logfile - [string] Filename of blf or log file (include path unless file is in current working directory). srate - sampling rate (in Hz) rtfile_arg - [string] Filename of a reaction time file (include path unless file is in current working directory). If blfORbdf_file is a bdf file, rtfile_arg will be ignored and a new rt file will be generated from the bdf file. Outputs: blf_info - A matrix indicating what bins each blf event belongs to. Each column corresponds to a different bin, each row a different blf item. bin_LongDesc - A cell array of strings describing each bin. blffile - The name of blf file used ('temp.blf' if a new blf file was created) blf_evnum - The log event number of each blf item. rtfile - The name of reaction time file used ('temp.rt' if a new rt file was created) cal_bin - The bin containing calibration pulses (only one bin is allowed to contain calibration pulses). cal_bin=[] if there is not a bin with a condition code of 0. cond_LongDesc - A cell array of strings describing each condition code. Note that a condition code of 0 is ignored, since it is presumed to correspond to calibration pulses. bin_CondCode - An integer array indicating the condition code of each bin described in bin_LongDesc (i.e., bin_LongDesc(1) has a condition code of bin_CondCode(1)) Author: David Groppe Kutaslab, 8/2009
0001 function [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]=import_blf(blfORbdf_file,logfile,srate,rtfile_arg) 0002 % import_blf() - Reads a blf or bdf file into Matlab for use by crw2set.m. 0003 % 0004 % 0005 % Usage: 0006 % >> [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, bin_CondCode]=import_blf(blfORbdf_file,logfile,srate,rtfile_arg) 0007 % 0008 % 0009 % Required Global Variable: 0010 % VERBLEVEL - matlabMK level of verbosity (i.e., tells functions how much 0011 % to report about what they're doing during runtime) 0012 % 0013 % 0014 %Inputs: 0015 % blfORbdf_file - [string] Filename of blf or bdf file (include path unless 0016 % file is in current working directory). 0017 % logfile - [string] Filename of blf or log file (include path unless 0018 % file is in current working directory). 0019 % srate - sampling rate (in Hz) 0020 % rtfile_arg - [string] Filename of a reaction time file (include path unless 0021 % file is in current working directory). If blfORbdf_file 0022 % is a bdf file, rtfile_arg will be ignored and a new rt 0023 % file will be generated from the bdf file. 0024 % 0025 %Outputs: 0026 % blf_info - A matrix indicating what bins each blf event belongs to. 0027 % Each column corresponds to a different bin, each row a 0028 % different blf item. 0029 % bin_LongDesc - A cell array of strings describing each bin. 0030 % blffile - The name of blf file used ('temp.blf' if a new blf file 0031 % was created) 0032 % blf_evnum - The log event number of each blf item. 0033 % rtfile - The name of reaction time file used ('temp.rt' if a new 0034 % rt file was created) 0035 % cal_bin - The bin containing calibration pulses (only one bin is 0036 % allowed to contain calibration pulses). cal_bin=[] if there 0037 % is not a bin with a condition code of 0. 0038 % cond_LongDesc - A cell array of strings describing each condition code. 0039 % Note that a condition code of 0 is ignored, since it is 0040 % presumed to correspond to calibration pulses. 0041 % bin_CondCode - An integer array indicating the condition code of each 0042 % bin described in bin_LongDesc (i.e., bin_LongDesc(1) has 0043 % a condition code of bin_CondCode(1)) 0044 % 0045 % Author: 0046 % David Groppe 0047 % Kutaslab, 8/2009 0048 0049 0050 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%% 0051 % 0052 % 11/23/09 Added cond_LongDesc output -DG 0053 % 0054 % 11/2/10 bin_CondCode added to keep track of which condition code 0055 % a bin belongs to 0056 0057 %GLOBAL 0058 global VERBLEVEL 0059 0060 0061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0062 % III. IMPORT BLF FILE INFORMATION 0063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0064 %Figure out if bdf of blf file was specified 0065 tag=blfORbdf_file(length(blfORbdf_file)-2:end); 0066 if strcmpi(tag,'bdf') %create blf file and rt file 0067 %remove temp.blf and temp.rt files in case they exist 0068 [s,w]=unix('rm -f temp.blf'); 0069 [s,w]=unix('rm -f temp.rt'); 0070 0071 VerbReport(sprintf(['Creating temporary blf file and reaction' ... 0072 ' time file using: %s'],blfORbdf_file),1,VERBLEVEL); 0073 cdbl_cmnd=sprintf('cdbl %s %s temp.blf %d -v1 -rt temp.rt',blfORbdf_file, ... 0074 logfile,srate); 0075 VerbReport(cdbl_cmnd,3,VERBLEVEL); 0076 [s, w]=unix(cdbl_cmnd); 0077 if s~=0, 0078 disp('Could not create blf file.'); 0079 error('According to cdbl: %s',w); 0080 end 0081 blffile='temp.blf'; 0082 if ~isempty(rtfile_arg) 0083 msg=sprintf('The RT file you specified, %s, will NOT be used ',rtfile_arg); 0084 msg=[msg 'because you specified a bdf file, NOT a blf file.']; 0085 msg=[msg ... 0086 sprintf(['\nThe temporary rt file, temp.rt, produced via' ... 0087 ' your bdf file will be used instead.\n'])]; 0088 watchit(msg); 0089 end 0090 rtfile='temp.rt'; 0091 elseif strcmpi(tag,'blf') %blf file specified 0092 blffile=blfORbdf_file; 0093 if ~isempty(rtfile_arg) 0094 rtfile=rtfile_arg; 0095 else 0096 rtfile=[]; 0097 end 0098 else 0099 error(sprintf(['The argument blfORbdf_file must end in "blf" or "bdf".' ... 0100 ' You gave it the value of %s.'],blfORbdf_file)); 0101 end 0102 0103 VerbReport(sprintf('Getting bin information from %s', blffile), 1, VERBLEVEL); 0104 0105 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0106 % III.A NAB THE NUMBER OF BINS IN BLF (FIRST LINE) 0107 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0108 fid_blf = fopen(blffile,'r'); 0109 line = fgetl(fid_blf); 0110 num_bins = str2num(line); 0111 0112 0113 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0114 % III.B NAB CONDITION AND BIN CODES/DESCRIPTIONS 0115 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0116 cal_bin=[]; %bin with calibration pulses 0117 BinCount = 0; 0118 bin_CondCode=[]; 0119 bin_LongDesc = {}; 0120 cond_LongDesc={}; 0121 while (BinCount < num_bins) 0122 currline = []; 0123 currline = fgetl(fid_blf); 0124 0125 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0126 % III.B.1 PARSE CD = CONDITION CODES/DESC 0127 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0128 if strcmp('CD',strtok(currline)) 0129 currExpCond = []; 0130 currExpCondDesc = []; 0131 [dummy,currExpCond] = strtok(currline); 0132 currExpCond = str2num(currExpCond); 0133 0134 % READ NEXT LINE FOR DESCRIPTION 0135 currline = []; 0136 currline = fgetl(fid_blf); 0137 currExpCondDesc = currline; 0138 VerbReport(sprintf('Current Exp Condition (Code %d): %s', currExpCond, currExpCondDesc),2,VERBLEVEL); 0139 if currExpCond, 0140 %Note: this ignores Condition Code of 0, since this assumed to be 0141 %just calibration pulses 0142 cond_LongDesc{currExpCond}=currExpCondDesc; 0143 end 0144 end; 0145 0146 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0147 % III.B.2 PARSE SD (I.E. BIN) CODES/DESC 0148 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0149 if strcmp('SD',strtok(currline)) 0150 currBinNum = []; 0151 currBinDesc = []; 0152 [dummy,currBinNum] = strtok(currline); 0153 currBinNum = str2num(currBinNum); 0154 0155 % READ NEXT LINE FOR DESCRIPTION 0156 currline = []; 0157 currline = fgetl(fid_blf); 0158 currBinDesc = currline; 0159 0160 VerbReport(sprintf('Current Bin (#%2d): %s', currBinNum, ... 0161 currBinDesc),2,VERBLEVEL); 0162 if ~currExpCond, 0163 if ~isempty(cal_bin) 0164 error('Your bdf/blf file appears to have more than one cal pulse bin. You can have only one.'); 0165 else 0166 cal_bin=currBinNum; 0167 VerbReport(sprintf([' Not importing this bin as an EEGLAB event because it contains' ... 0168 ' only cal pulses.']),2,VERBLEVEL); 0169 VerbReport(sprintf([' I presume this because of the condition code of' ... 0170 ' 0.']),2,VERBLEVEL); 0171 end 0172 else 0173 bin_LongDesc{length(bin_LongDesc)+1} = currBinDesc; 0174 bin_CondCode(length(bin_LongDesc))=currExpCond; 0175 end 0176 BinCount = BinCount + 1; 0177 end 0178 end 0179 0180 0181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0182 % III.C HANDLE EVENT LINES W/ BINS SPECIFIED INCLUDING CALS 0183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0184 0185 % SLURP EVENT/BIN DATA FROM BLF ... 0186 % blf_info has 1 in EACH COLUMN 0187 % CORRESPONDING TO THE BIN IT BELONGS IN, ZERO ELSE 0188 % blf_info *ONLY* HAS ROWS FOR EVENTS WHICH GO INTO BINS 0189 blf_info = zeros(1,BinCount); 0190 blf_evnum= 0; 0191 blfind = 1; 0192 while ~feof(fid_blf) % parse bin list 0193 line = []; 0194 line = fgetl(fid_blf); 0195 tmp = []; 0196 tmp = str2num(line); 0197 0198 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0199 % IF THERE IS MORE THAN JUST AN EVENT CODE ON THE LINE ... 0200 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0201 if length(tmp) > 1 0202 evnum = []; 0203 0204 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0205 % NAB THE EVENT NUMBER ... THIS IS REDUNDANT W/ EVENT NUMBER 0206 % IN THE LOG FILE BUT RETAINED TO CROSS-CHECK THAT THE 0207 % LOG INFO AND BLF INFO IS ALIGNED IN THE COMBINED EP_INFO 0208 % DATA ARRAY 0209 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0210 evnum = tmp(1); 0211 blf_evnum(blfind) = evnum; 0212 0213 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0214 % FOR EACH BIN THIS EVENT GOES INTO, SET BIN_INFO COLUMN = 1. 0215 % COLUMN INDEX = BIN NUMBER FROM BLF + 1 FOR THE ZER0-TO-1-BASE 0216 % CONVERSION 0217 % EX. BLF BIN 3 -> BIN_INFO COLUMN 4 = 3 + 1 0218 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0219 for i=2:size(tmp, 2) 0220 blf_info(blfind,tmp(i)+1) = 1; 0221 end; 0222 0223 % INCREMENT TO NEXT LINE OF BLF 0224 blfind = blfind + 1; 0225 end; 0226 end; 0227 fclose(fid_blf);