⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prt_collapse.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function hfile = prt_Collapse(hfile, cfrom, cto, ncol)
% PRT::AddCond  - add a condition to a PRT file
%
% FORMAT:       [prt] = prt.Collapse(cfrom, newname [, newcolor])
%
% Input fields:
%
%       cfrom       from-condition specification
%                   can be either a 1xN double array or a regexp pattern
%       newname     name for collapsed condition
%       newcolor    1x3 optional color (will be mixed otherwise)
%
% Output fields:
%
%       prt         altered PRT
%
% Examples:
%
%   prt.Collapse([1, 3, 5, 7], 'Odd conditions', [255, 0, 0]);
%   prt.Collapse([2, 3, 4, 5], 'Even conditions', [0, 255, 0]);
%
%  - or -
%
%   prt.Collapse('.*odd.*',  'Odd conditions');
%   prt.Collapse('.*even.*', 'Even conditions');

% Version:  v0.7b
% Build:    7090213
% Date:     Sep-02 2007, 1:22 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 3 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'prt') || ...
    isempty(cfrom) || ...
    isempty(cto) || ...
   ~ischar(cto)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);

% get number of conditions
ncon = numel(bc.Cond);

% calc new color ?
if nargin > 3 && ...
    isa(ncol, 'double') && ...
    numel(ncol) == 3 && ...
    ~any(isinf(ncol) | isnan(ncol) | ncol < 0 | ncol > 255)

    % accept
    ncol = fix(ncol(:)');
    usenc = true;
else
    ncol = [0, 0, 0];
    usenc = false;
end

% what kind of input
if isa(cfrom, 'double')
    
    % check min, max, etc...
    cfrom = cfrom(:)';
    if any(isinf(cfrom) | isnan(cfrom) | cfrom < 1 | cfrom > ncon | fix(cfrom) ~= cfrom)
        error( ...
            'BVQXfile:BadArgument', ...
            'Bad from specification given.' ...
        );
    end
    
    % get conditions
    selected = bc.Cond(cfrom);
    
    % combine onsets
    oos = zeros(0, 2);
    for cc = 1:numel(cfrom)
        oos = [oos; selected(cc).OnOffsets];
        if ~usenc
            ncol = ncol + selected(cc).Color;
        end
    end
    if ~usenc
        ncol =ncol / numel(cfrom);
    end
    
    % sort onsets
    [ooso{1:2}] = sort(oos(:, 1));
    oos = oos(ooso{2}, :);
    
    % get only first one
    selected = selected(1);
    selected.ConditionName = {cto};
    selected.NrOfOnOffsets = size(oos, 1);
    selected.OnOffsets = oos;
    selected.Color = fix(ncol);
    
    % put back into PRT
    bc.Cond(cfrom(1)) = selected;
    
    % remove collapsed
    bc.Cond(cfrom(2:end)) = [];
    
    % set new number of conditions
    bc.NrOfConditions = numel(bc.Cond);
    
% character input
elseif ischar(cfrom) && ...
   ~isempty(cfrom) && ...
    size(cfrom, 2) == numel(cfrom)
    
    % create empty condition array
    newconds = bc.Cond;
    newconds(:) = [];

    % build list of matches
    condlist = struct;
    condnums = struct;
    for cc = 1:numel(bc.Cond)
        
        % get condition name
        condname = bc.Cond(cc).ConditionName{1};
        
        % match against pattern
        [cdmatch{1:3}] = regexpi(condname, cfrom);
        cdmatch = cdmatch{3};
        
        % if no match, simply store in newconds!
        if isempty(cdmatch)
            newconds(end + 1) = bc.Cond(cc);
            continue;
        end
        
        % otherwise get name right!
        newcondname = regexprep(condname, cfrom, cto, 'ignorecase');
        
        % then create a valid label
        condtag = makelabel(newcondname);
        
        % and check whether it's already in the list
        if isfield(condlist, condtag)
            
            % get condition number to combine with
            cmbcnum = condlist.(condtag);
            
            % combine onsets
            newons = [ ...
                newconds(cmbcnum).OnOffsets; ...
                bc.Cond(cc).OnOffsets];
            
            % sort onsets
            ooso = cell(1, 2);
            [ooso{1:2}] = sort(newons(:, 1));
            newons = newons(ooso{2}, :);
            
            % write onsets into newconds
            newconds(cmbcnum).OnOffsets = newons;
            
            % recalculate color
            if ~usenc
                newconds(cmbcnum).Color = ...
                    (condnums.(condtag) * newconds(cmbcnum).Color + ...
                     bc.Cond(cc).Color) / ...
                    (condnums.(condtag) + 1);
            end
            
        % otherwise add it as well
        else
            newconds(end + 1) = bc.Cond(cc);
            
            % and exchange the name
            newconds(end).ConditionName{1} = newcondname;
            
            % and set the correct condition number!
            condlist.(condtag) = numel(newconds);
            condnums.(condtag) = 1;
            
            % set new color ?
            if usenc
                newconds(end).Color = ncol;
            end
        end
    end
    
    % fix colors to integral numbers
    if ~usenc
        for cc = 1:numel(newconds)
            newconds(cc).Color = fix(newconds(cc).Color);
        end
    end
    
    % set newconds into content
    bc.Cond = newconds;
    bc.NrOfConditions = numel(newconds);
    
% reject the rest for the moment
else
    error( ...
        'BVQXfile:NotYetImplemented', ...
        'Collapsing over names not yet implemented.' ...
    );
end

% set new content
bvqxfile_setcont(hfile.L, bc);

⌨️ 快捷键说明

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