Home > bioelectromagnetism > eeg_open.m

eeg_open

PURPOSE ^

eeg_open - function to handle various eeg_load commands

SYNOPSIS ^

function [p] = eeg_open(p,parent)

DESCRIPTION ^

 eeg_open - function to handle various eeg_load commands
 
 Usage: [p] = eeg_open(p,[parentgui])
 
 p is a parameter structure. See eeg_toolbox_defaults for more 
 information on this parameter structure.
 
 In this function, p must contain the fields:
 
 p.volt.path - the directory location of the file to load
 p.volt.file - the name of the file to load
 p.volt.type - the file format string, one of:
 
 'ASCII'
 'EMSE'
 'Scan4x'
 'Scan3x'
 'Matlab'

 These are the only file types currently supported. 
 See functions eeg_load* for details.
 
 The most important return value is the ERP data in 
 p.volt.data.  If the file format is scan4x, various 
 ERP parameters are returned also.
 
 If the ASCII or Matlab type is given, the routine will try 
 to load an associated variance file.  This file must be 
 located in the same path, with the same file name as the 
 voltage file, but the file extension should be '.var'
 
 See also: EEG_LOAD, EEG_LOAD_ASCII, EMSE_READ_AVG,
           EEG_LOAD_SCAN4_AVG, EEG_LOAD_SCAN3_AVG

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [p] = eeg_open(p,parent)
0002 
0003 % eeg_open - function to handle various eeg_load commands
0004 %
0005 % Usage: [p] = eeg_open(p,[parentgui])
0006 %
0007 % p is a parameter structure. See eeg_toolbox_defaults for more
0008 % information on this parameter structure.
0009 %
0010 % In this function, p must contain the fields:
0011 %
0012 % p.volt.path - the directory location of the file to load
0013 % p.volt.file - the name of the file to load
0014 % p.volt.type - the file format string, one of:
0015 %
0016 % 'ASCII'
0017 % 'EMSE'
0018 % 'Scan4x'
0019 % 'Scan3x'
0020 % 'Matlab'
0021 %
0022 % These are the only file types currently supported.
0023 % See functions eeg_load* for details.
0024 %
0025 % The most important return value is the ERP data in
0026 % p.volt.data.  If the file format is scan4x, various
0027 % ERP parameters are returned also.
0028 %
0029 % If the ASCII or Matlab type is given, the routine will try
0030 % to load an associated variance file.  This file must be
0031 % located in the same path, with the same file name as the
0032 % voltage file, but the file extension should be '.var'
0033 %
0034 % See also: EEG_LOAD, EEG_LOAD_ASCII, EMSE_READ_AVG,
0035 %           EEG_LOAD_SCAN4_AVG, EEG_LOAD_SCAN3_AVG
0036 %
0037 
0038 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $
0039 
0040 % Licence:  GNU GPL, no express or implied warranties
0041 % History:  02/2002, Darren.Weber_at_radiology.ucsf.edu
0042 %           04/2002, Darren.Weber_at_radiology.ucsf.edu
0043 %                    added variance handling
0044 %           08/2002, Darren.Weber_at_radiology.ucsf.edu
0045 %                    added EMSE avg handling
0046 %                    added interpolation of zero point option
0047 %
0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0049 
0050 if ~exist('p','var'),[p] = eeg_toolbox_defaults; end
0051 
0052 eegversion = '$Revision: 1.1 $';
0053 fprintf('EEG_OPEN [v %s]\n',eegversion(11:15)); tic;
0054 
0055 
0056 [path,name,ext] = fileparts(strcat(p.volt.path, filesep, p.volt.file));
0057 file = fullfile(path,[name ext]);
0058 
0059 if ~isequal(exist(file),2),
0060   lookfile = which(file);
0061   if isempty(lookfile),
0062     msg = sprintf('...cannot locate %s\n', file);
0063     error(msg);
0064   else
0065     file = lookfile;
0066   end
0067 end
0068 
0069 type = lower(p.volt.type)
0070 
0071 switch type,
0072   
0073   case 'ascii',
0074     
0075     [ p.volt.data, p.volt.var ] = eeg_load_ascii(file);
0076     
0077   case 'emse',
0078     
0079     avg = emse_read_avg(file);
0080     
0081     % load variance data?
0082     if isfield(avg,'volt'),
0083       if isempty(avg.volt),
0084         error('failed to read file.');
0085       end
0086     else
0087       error('failed to read file.');
0088     end
0089     
0090     p.volt.data          = avg.volt;
0091     %p.volt.var           = avg.variance;
0092     %p.volt.channelNames  = avg.chan_names;
0093     p.volt.channels      = avg.channels;
0094     p.volt.points        = avg.pnts;
0095     p.volt.sampleMsec    = avg.rate;
0096     p.volt.epochStart    = avg.xmin;
0097     %p.volt.epochEnd      = avg.xmax;
0098     p.volt.sampleHz      = 1000 / avg.rate;
0099     %p.volt.sweeps        = avg.nsweeps;
0100     
0101     
0102   case 'scan4x_cnt',
0103     
0104     p.cnt = eeg_load_scan4_cnt(file);
0105     
0106   case 'scan4x',
0107     
0108     avg = eeg_load_scan4_avg(file);
0109     
0110     if ~isfield(avg,'data'),
0111       msg = sprintf('...failed to load scan4.x avg file:\n... %s\n',file);
0112       error(msg);
0113     end
0114     if isequal([avg.header.domain],1),
0115       msg = sprintf('...cannot open frequency domain file:\n... %s\n',file);
0116       error(msg);
0117     end
0118     
0119     p.volt.points = avg.header.pnts;
0120     p.volt.sampleHz = avg.header.rate;
0121     p.volt.sampleMsec = 1000/ avg.header.rate;
0122     p.volt.channels = avg.header.nchannels;
0123     p.volt.epochStart = avg.header.xmin * 1000; % convert to msec
0124     p.volt.epochEnd   = avg.header.xmax * 1000; % convert to msec
0125     p.volt.sweeps = avg.header.acceptcnt;
0126     
0127     ampData = [avg.data.samples]; % elect in col, samples in row
0128     baseline = repmat([avg.electloc.baseline],p.volt.points,1);
0129     calibration = repmat([avg.electloc.calib],p.volt.points,1);
0130     n = repmat([avg.electloc.n],p.volt.points,1);
0131     % Convert to uV
0132     p.volt.data = ( ampData - baseline ) .* calibration ./ n;
0133     p.volt.var = [avg.variance.samples];
0134     
0135     
0136   case 'scan3x',
0137     
0138     avg = eeg_load_scan3_avg(file);
0139     
0140     if isfield(avg,'signal'),
0141       p.volt.data          = avg.signal;
0142       p.volt.var           = avg.variance;
0143       p.volt.channelNames  = avg.chan_names;
0144       p.volt.points        = avg.pnts;
0145       p.volt.sampleHz      = avg.rate;
0146       p.volt.epochStart    = avg.xmin;
0147       p.volt.epochEnd      = avg.xmax;
0148       p.volt.sampleMsec    = avg.rate / 1000;
0149       p.volt.sweeps        = avg.nsweeps;
0150     else
0151       msg = sprintf('...failed to load scan3.x datafile: %s\n',file);
0152       error(msg);
0153     end
0154     
0155   case 'matlab',
0156     
0157     p.volt.data = eeg_load(file);
0158     % Attempt to load associated variance file
0159     varfile = fullfile(path,strcat(name,'.var'));
0160     if isequal(exist(varfile),2),
0161       p.volt.var = eeg_load(varfile);
0162     else
0163       lookfile = which(varfile);
0164       if isempty(lookfile),
0165         fprintf('...cannot locate Matlab variance file:\n... %s\n', varfile);
0166       else
0167         varfile = lookfile;
0168         p.volt.var = eeg_load(varfile);
0169       end
0170     end
0171     
0172   otherwise,
0173     msg = sprintf('\nPlease specify voltage data type: ASCII | EMSE | Scan4x | Scan3x | Matlab?\n\n');
0174     error(msg);
0175 end
0176 
0177 % Try to arrange electrodes in columns (assuming more sample points than electrodes)
0178 s = size(p.volt.data);
0179 if s(1) < s(2),
0180   fprintf('...rotating voltage data from %s : ',[name ext]);
0181   p.volt.data = p.volt.data';
0182   s = size(p.volt.data);
0183   fprintf('%d rows, %d cols\n', s(1), s(2));
0184 end
0185 p.volt.channels = s(2);
0186 p.volt.points = s(1);
0187 
0188 s = size(p.volt.var);
0189 if s(1) < s(2),
0190   if isequal(p.volt.type,'ASCII'), file = varfile; end
0191   if isequal(p.volt.type,'Matlab'), file = varfile; end
0192   fprintf('...rotating variance data from:\n... %s : ',varfile);
0193   p.volt.var = p.volt.var';
0194   s = size(p.volt.var);
0195   fprintf('%d rows, %d cols\n', s(1), s(2));
0196 end
0197 
0198 
0199 % Verify that essential ERP parameters are set
0200 if isempty(p.volt.sampleHz) | isempty(p.volt.epochStart) | isempty(p.volt.epochEnd),
0201   
0202   if exist('parent','var'),
0203     if ~isempty(parent),
0204       %help = helpdlg('Please specify ERP parameters as follows...','EEG OPEN HELP');
0205       %movegui(help,'center'); waitfor(help);
0206       
0207       data = get(parent,'UserData');
0208       data.p = p;
0209       set(parent,'UserData',data);
0210       
0211       tmpgui = gui_eeg_ascii_parameters(parent);
0212       
0213       data = get(parent,'UserData');
0214      [p] = data.p; 
0215       clear data parent tmpgui;
0216     end
0217   else
0218     fprintf('...please specify ERP parameters in p structure.\n');
0219   end
0220 end
0221 
0222 
0223 % --- Setup data structures for timing array
0224 
0225 % Interpolate the Zero value
0226 if p.volt.interpZero,
0227   
0228   if p.volt.epochStart & p.volt.epochEnd & p.volt.sampleMsec,
0229     
0230     if mod(p.volt.epochStart,fix(p.volt.epochStart)) < .1,
0231       % round the epochStart value, if its remainder is within .1 msec
0232       p.volt.epochStart = fix(p.volt.epochStart);
0233     end
0234     
0235     p.volt.timeArray = [p.volt.epochStart:p.volt.sampleMsec:p.volt.epochEnd]';
0236     
0237     timeNonZero = find(p.volt.timeArray);
0238     timeZero = find(p.volt.timeArray == 0);
0239     
0240     volt = p.volt.data;
0241     
0242     InterpZero = interp1( p.volt.timeArray(timeNonZero), volt, 0, 'cubic' );
0243     volt = [volt(1:timeZero-1,:); InterpZero; volt(timeZero:end,:)];
0244     p.volt.data = volt; 
0245     
0246     clear InterpZero timeNonZero timeZero volt;
0247   end
0248 else
0249   p.volt.timeArray = [1:p.volt.points]';
0250   if and(p.volt.epochStart,p.volt.sampleMsec),
0251     t = p.volt.epochStart;
0252     for i=1:p.volt.points,
0253       p.volt.timeArray(i,1) = t;
0254       t = t + p.volt.sampleMsec;
0255     end
0256   end   
0257 end
0258 
0259 p.volt.points = size(p.volt.data,1);
0260 
0261 t = toc; fprintf('...done (%6.2f sec).\n\n',t);
0262 
0263 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003