pop_importepoch.m

来自「含有多种ICA算法的eeglab工具箱」· M 代码 · 共 526 行 · 第 1/2 页

M
526
字号
         { 'Style', 'text', 'string', '           Field name containing time-locking event type(s)', 'horizontalalignment', 'right', ...
                                      'tooltipstring', helpstrtype },  ...
  		 { 'Style', 'edit', 'string', '' }, ...
         { 'Style', 'text', 'string', 'NOTE', 'tooltipstring', helpstrtype }, ...
         { } ...
         { 'Style', 'text', 'string', 'Latency time unit rel. to seconds. Ex: ms -> 1E-3', 'horizontalalignment', 'left' }, { 'Style', 'edit', 'string', '1' }, { } ...         
         { 'Style', 'text', 'string', 'Number of file header lines to ignore', 'horizontalalignment', 'left' }, { 'Style', 'edit', 'string', '0' }, { },...        
         { 'Style', 'text', 'string', 'Remove old epoch and event info (set = yes)', 'horizontalalignment', 'left' }, { 'Style', 'checkbox', 'value', isempty(EEG.event) }, { } };         
    result = inputgui( geometry, uilist, 'pophelp(''pop_importepoch'');', 'Import epoch info (data epochs only) -- pop_importepoch()');
    if length(result) == 0, return; end;

    filename    = result{1};
    fieldlist   = parsetxt( result{2} );
    options = {};
    if ~isempty( result{3}), options = { options{:} 'latencyfields' parsetxt( result{3} ) }; end; 
    if ~isempty( result{4}), options = { options{:} 'durationfields' parsetxt( result{4} ) }; end; 
    if ~isempty( result{5}), options = { options{:} 'typefield' result{5} }; end; 
    if ~isempty( result{6}), options = { options{:} 'timeunit' eval(result{6}) }; end; 
    if ~isempty( result{7}), options = { options{:} 'headerlines' eval(result{7}) }; end; 
    if ~result{8}, options = { options{:} 'clearevents' 'off'}; end; 
else 
    if ~isempty(varargin) & ~isstr(varargin{1})
        % old call compatibility
        options = { 'latencyfields' varargin{1} };
        if nargin > 4
            options = { options{:} 'timeunit' varargin{2} }; 
        end; 
        if nargin > 5
            options = { options{:} 'headerlines' varargin{3} }; 
        end; 
        if nargin > 6
            options = { options{:} 'clearevents' fastif(varargin{4}, 'on', 'off') }; 
        end; 
    else
        options = varargin;
    end;
end;

g = finputcheck( options, { 'typefield'      'string'   []       ''; ...
                            'latencyfields'  'cell'     []       {}; ...
                            'durationfields' 'cell'     []       {}; ...
                            'timeunit'       'real'     [0 Inf]  1/EEG.srate; ...
                            'headerlines'    'integer'  [0 Inf]  0; ...
                            'clearevents'    'string'   {'on' 'off'}  'on'}, 'pop_importepoch');
if isstr(g), error(g); end;

% check duration field
% --------------------
if ~isempty(g.durationfields)
    if length(g.durationfields) ~= length(g.latencyfields) 
        error( [ 'If duration field(s) are defined, their must be as many duration' 10 ...
              'fields as there are latency fields (or enter 0 instead of a field for no duration' ]);
    end;
else
    for index = 1:length(g.latencyfields) 
        g.durationfields{index} = 0;
    end;
end;

% convert filename
% ----------------
fprintf('Pop_importepoch: Loading file or array...\n');
if isstr(filename)
	% check filename
	% --------------
	if exist(filename) == 2 & evalin('base', ['exist(''' filename ''')']) == 1
		disp('Pop_importepoch WARNING: FILE AND ARRAY WITH THE SAME NAME, LOADING FILE');
	end;
    values = load_file_or_array( filename, g.headerlines );
else
    values = filename;
    filename = inputname(2);
end;

% check parameters
% ----------------
if size(values,1) < size(values,2), values = values'; end;
if length(fieldlist) ~= size(values,2)
    values = values';
    if length(fieldlist) ~= size(values,2)
        error('There must be as many field names as there are columsn in the file/array');
    end;
end;
if ~iscell(fieldlist)
    otherfieldlist = { fieldlist };
    fieldlist = { fieldlist };
end;
otherfieldlist = setdiff( fieldlist, g.latencyfields);
otherfieldlist = setdiff( otherfieldlist, g.typefield);
for index = 1:length(g.durationfields)
    if isstr(g.durationfields{index})
        otherfieldlist = setdiff( otherfieldlist, g.durationfields{index});
    end;
end;
if size(values,1) ~= EEG.trials
    error( [ 'Pop_importepoch() error: the number of rows in the input file/array does' 10 ... 
             'not match the number of trials. Maybe you forgot to specify the file header length?' ]);
end;    

% create epoch array info
% -----------------------
if iscell( values )
    for indexfield = 1:length(fieldlist)
        for index=1:EEG.trials
            eval( ['EEG.epoch(index).' fieldlist{ indexfield } '=values{ index, indexfield };'] );
        end;
    end;    
else
    for indexfield = 1:length(fieldlist)
        for index=1:EEG.trials
            eval( ['EEG.epoch(index).' fieldlist{ indexfield } '=values( index, indexfield);'] );
        end;
    end;    
end;

if isempty( EEG.epoch )
    error('Pop_importepoch: cannot process empty epoch structure');
end;
epochfield = fieldnames( EEG.epoch );

% determine the name of the non latency fields
% --------------------------------------------
tmpfieldname = {};
for index = 1:length(otherfieldlist)
    if isempty(strmatch( otherfieldlist{index}, epochfield ))
         error(['Pop_importepoch: field ''' otherfieldlist{index} ''' not found']);
    end;
    switch otherfieldlist{index}
       case {'type' 'latency'}, tmpfieldname{index} = [ 'epoch' otherfieldlist{index} ];
       otherwise,               tmpfieldname{index} = otherfieldlist{index};
    end;   
end;

if ~isempty(EEG.event)
    if ~isfield(EEG.event, 'epoch')
        g.clearevents = 'on';
        disp('Pop_importepoch: cannot add events to a non-epoch event structure, erasing old epoch structure');
    end;
end;
if strcmpi(g.clearevents, 'on')
    if ~isempty(EEG.event)
        fprintf('Pop_importepoch: deleting old events if any\n');
    end;
    EEG.event = [];
else 
    fprintf('Pop_importepoch: appending new events to the existing event array\n');
end;
           
% add time locking event fields
% -----------------------------
if EEG.xmin <= 0
    fprintf('Pop_importepoch: adding automatically Time Locking Event (TLE) events\n');
    if ~isempty(g.typefield)
        if isempty(strmatch( g.typefield, epochfield )) 
            error(['Pop_importepoch: type field ''' g.typefield ''' not found']);
        end;
    end;
    for trial = 1:EEG.trials
        EEG.event(end+1).epoch = trial; 
        if ~isempty(g.typefield)
            eval( ['EEG.event(end).type = EEG.epoch(trial).' g.typefield ';'] );
        else 
            EEG.event(end).type = 'TLE';
        end;
        EEG.event(end).latency  = -EEG.xmin*EEG.srate+1+(trial-1)*EEG.pnts;
        EEG.event(end).duration = 0;
    end;
end;

% add latency fields
% ------------------
for index = 1:length(g.latencyfields)
    if isempty(strmatch( g.latencyfields{index}, epochfield )) 
         error(['Pop_importepoch: latency field ''' g.latencyfields{index} ''' not found']);
    end;
    for trials = 1:EEG.trials
        EEG.event(end+1).epoch  = trials; 
        EEG.event(end).type     = g.latencyfields{index};
        EEG.event(end).latency  = (getfield(EEG.epoch(trials), g.latencyfields{index})*g.timeunit-EEG.xmin)*EEG.srate+1+(trials-1)*EEG.pnts;
        if g.durationfields{index} ~= 0 & g.durationfields{index} ~= '0'
            EEG.event(end).duration = getfield(EEG.epoch(trials), g.durationfields{index})*g.timeunit*EEG.srate;
        else
            EEG.event(end).duration = 0;
        end;
    end;
end;

% add non latency fields
% ----------------------
if ~isfield(EEG.event, 'epoch') % no events added yet
    for trial = 1:EEG.trials
        EEG.event(end+1).epoch = trial;
    end;
end;
for indexevent = 1:length(EEG.event)
    if ~isempty( EEG.event(indexevent).epoch )
        for index2 = 1:length(tmpfieldname)
            eval( ['EEG.event(indexevent).' tmpfieldname{index2} ' = EEG.epoch(EEG.event(indexevent).epoch).' otherfieldlist{index2} ';' ] );
    	end;
    end;
end;

% adding desciption to the fields
% -------------------------------
if ~isfield(EEG, 'eventdescription' ) | isempty( EEG.eventdescription )
	allfields = fieldnames(EEG.event);
    EEG.eventdescription{strmatch('epoch', allfields, 'exact')} = 'Epoch number';
	if ~isempty(strmatch('type', allfields)), EEG.eventdescription{strmatch('type', allfields)} = 'Event type'; end;
	if ~isempty(strmatch('latency', allfields)), EEG.eventdescription{strmatch('latency', allfields)} = 'Event latency'; end;
	if ~isempty(strmatch('duration', allfields)), EEG.eventdescription{strmatch('duration', allfields)} = 'Event duration'; end;
end;

% checking and updating events
% ----------------------------
EEG = pop_editeventvals( EEG, 'sort', { 'epoch', 0 } ); % resort fields
EEG = eeg_checkset(EEG, 'eventconsistency');
EEG = eeg_checkset(EEG, 'makeur');

% generate the output command
% ---------------------------
if isempty(filename) & nargout == 2
    disp('Pop_importepoch: cannot generate command string'); return;
else 
	com = sprintf('%s = pop_importepoch( %s, ''%s'', %s);', inputname(1), inputname(1), ...
                  filename, vararg2str( { fieldlist options{:} }));
end;

% interpret the variable name
% ---------------------------
function array = load_file_or_array( varname, skipline );

    if exist( varname ) == 2
        if exist(varname) ~= 2, error( [ 'Set error: no filename ' varname ] ); end;

		fid=fopen(varname,'r','ieee-le');
		if fid<0, error( ['Set error: file ''' varname ''' found but error while opening file'] ); end;  

		for index=1:skipline	fgetl(fid); end; % skip lines ---------
        inputline = fgetl(fid);
        linenb = 1;
        while inputline~=-1
            colnb = 1;
            while ~isempty(deblank(inputline))
                [tmp inputline] = strtok(inputline);
                tmp2 = str2num( tmp );
                if isempty( tmp2 ), array{linenb, colnb} = tmp;
                else                array{linenb, colnb} = tmp2;
                end;
                colnb = colnb+1;
            end;
            inputline = fgetl(fid);
            linenb = linenb +1;
        end;        
                
		fclose(fid);

    else % variable in the global workspace
         % --------------------------
         array = evalin('base', varname);
    end;     
return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?