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

📄 tdlocal.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
📖 第 1 页 / 共 2 页
字号:
    btype = 0;
end

% default set label components (empty) and return string (*)
rstring  = '*';
rxlabelc = [];
xlabel   = 'l_empty';
xlabelc  = zeros(1, 5);

% different lookup types
switch (round(rtype))

    % Talairach label
    case {2}

        % get lookup x,y,z coordinates and sizes
        xc = round(xc(1) + tbase_opts.xtrans);
        yc = round(yc(1) + tbase_opts.ytrans);
        zc = floor(zc(1));
        xm = tbase_opts.xsize;
        ym = tbase_opts.ysize;
        zn = tbase_opts.zmin;
        zm = tbase_opts.zmax;

        % check coordinates
        if  xc < 1 || ...
            xc > xm || ...
            yc < 1 || ...
            yc > ym || ...
            zc < zn || ...
            zc > zm
            return;
        end

        % find good z representation slice
        zc = tbase_opts.zlui(zc + 1 - zn);

        % get label index list
        lilist = double(squeeze(tbase_opts.lookup(xc, yc, zc, :)));
        lilset = lilist(lilist ~= 0);

        % any content
        if ~isempty(lilset)

            % reset output
            rstringc = {'*','*','*','*','*'};
            rxlabelc = [0 0 0 0 0];
            for lnum = lilset(:)'
                rstringc{tbase_opts.labels{lnum}{2}} = tbase_opts.labels{lnum}{1};
                rxlabelc(tbase_opts.labels{lnum}{2}) = lnum;
            end
            if btype
                rstring = {rstringc{1:5}};
            else
                rstring = [ ...
                    rstringc{1}, ',', ...
                    rstringc{2}, ',', ...
                    rstringc{3}, ',', ...
                    rstringc{4}, ',', ...
                    rstringc{5}];
            end
        end

    % Talairach cube lookup
    case {3}

        % only valid with 5 arguments, cs being double
        if nargin < 5 || ...
           ~isa(cs, 'double') || ...
            numel(cs) ~= 1
            error( ...
                'BVQXtools:TooFewArguments',...
                'Argument ''size'' missing.' ...
            );
        end

        % get good cs from [3..13]
        cs  = min(13, max(3, cs));
        cs  = floor(cs / 2);

        % create data holding structs
        lss = struct;
        lsn = struct;

        % iterate over all coordinates
        for xr = (xc-cs):(xc+cs)
            for yr = (yc-cs):(yc+cs)
                for zr = (zc-cs):(zc+cs)

                    % recursively call
                    [lc, lcf] = tdlocal(2, xr, yr, zr);

                    % check struct
                    if ~isfield(lss,lcf)
                        lss.(lcf) = 1;
                        lsn.(lcf) = lc;
                    else
                        lss.(lcf) = lss.(lcf) + 1;
                    end
                end
            end
        end

        % get filled in, distinct labels, init to zero (to sort by number)
        lt  = fieldnames(lsn);
        lti = zeros(size(lt));

        % iterate over fields (unique labels)
        for lout = 1:length(lt)
            lti(lout) = lss.(lt{lout});
        end

        % sort and init return string
        [ltii{1:2}] = sort(lti);
        ltii = ltii{2};
        rstring = '';

        % fill return string
        for lout = length(lt):-1:1
            rstring = sprintf('%s%d:%s:', rstring, ...
                lss.(lt{ltii(lout)}), ...
                lsn.(lt{ltii(lout)}));
        end

    % Nearest gray matter search
    case {5}

        % only valid with 5 arguments, cs being double
        if nargin < 5 || ...
           ~isa(cs, 'double') || ...
            numel(cs) ~= 2 || ...
            any(isinf(cs) | isnan(cs) | cs < 1 | cs > 10);
            error( ...
                'BVQXtools:TooFewArguments',...
                'Argument size missing or invalid.' ...
            );
        end

        % get good range for sphere
        xcs      = min(8.501, max(1.19, cs(1)));
        radius   = fix(xcs + 0.8);
        middle   = radius + 1;
        diameter = 2 * radius + 1;
        found(1:diameter, 1:diameter, 1:diameter) = 0;

        % check direct coordinate first
        tresult = tdlocal(2, xc, yc, zc);
        rstring = '*:*';

        % if found
        if strfind(lower(tresult), 'gray matter')
            if btype
                tresult = tdlocal(-2,xc,yc,zc);
                rstring = {{[xc,yc,zc], [0,0,0], 0, tresult}};
            else
                rstring = ['+0,+0,+0  (dist=0.000): ' tresult];
            end
            return;
        end

        % iterate over positions
        for xr = -radius:radius
            for yr = -radius:radius
                for zr = -radius:radius

                    % check distance to center
                    dist = sqrt(xr*xr + yr*yr + zr*zr);
                    if dist > xcs
                        continue;
                    end

                    % call tdlocal recursively
                    tresult = tdlocal(2,xc + xr, yc + yr, zc + zr);

                    % if gray matter found
                    if strfind(lower(tresult), 'gray matter')
                        found(middle + xr, middle + yr, middle + zr) = dist;
                    end
                end
            end
        end

        % no coordinate found
        rfound = find(found(:));
        if isempty(rfound)
            return;
        end

        % find distances
        rdist  = found(rfound);
        [rsort,rindex] = sort(rdist);

        % how many distinct labels at most?
        cs(2) = fix(cs(2) + 0.1);

        % init return string
        if btype
            rstring = {};
        else
            rstring = '';
        end

        % init struct
        lss = struct;

        % iterate over number of found occurrences
        radius = radius + 1;
        for rcount = 1:length(rsort)

            % get coordinate for index
            [rpx, rpy, rpz] = ind2sub(size(found), rfound(rindex(rcount)));
            mxc  = xc - radius + rpx;
            myc  = yc - radius + rpy;
            mzc  = zc - radius + rpz;

            % on requested output type
            if btype

                % call tdlocal again
                [tresult, lcf] = tdlocal(-2, mxc, myc, mzc);

                % put into output
                if ~isfield(lss, lcf)

                    % add to cell output
                    rstring{end+1} = {[mxc, myc, mzc], ...
                                      [rpx - radius, rpy - radius, rpz - radius], ...
                                       rsort(rcount), tresult};

                    % make field available and reduce cs(2)
                    lss.(lcf) = 1;
                    cs(2) = cs(2)-1;
                end

            % direct output
            else

                % call tdlocal again
                [tresult, lcf] = tdlocal(2, mxc, myc, mzc);

                % put into output
                if ~isfield(lss, lcf)
                    rstring = sprintf('%s%+d,%+d,%+d  (dist=%.3f): %s (coords=%d,%d,%d):', ...
                                      rstring, rpx - radius, rpy - radius, rpz - radius, ...
                                      rsort(rcount), tresult, ...
                                      mxc, myc, mzc);

                    % make field available and reduce cs(2)
                    lss.(lcf) = 1;
                    cs(2) = cs(2)-1;
                end
            end

            % leave if number of hits was found
            if cs(2) < 1
                break;
            end
        end

    % Talairach label search
    case {6}

        % prepare output
        rstring = [zeros(0,1), zeros(0,1), zeros(0,1)];

        % check second arg
        if ~ischar(xc) || ...
            isempty(xc)
            return;
        end

        % find matching strings
        ccs = lower(xc);
        ccm = find(strcmp(tbase_opts.plabels, ccs(:)'));
        if isempty(ccm)
            return;
        end

        % find matching voxels
        mvx = [];
        for lc = ccm
            mvx = union(mvx, find(any(tbase_opts.lookup == uint8(lc), 4)));
        end
        [mvx, mvy, mvz] = ind2sub( ...
            [tbase_opts.xsize, tbase_opts.ysize, tbase_opts.zsize], mvx);

        % create output array
        mvx = mvx - tbase_opts.xtrans;
        mvy = mvy - tbase_opts.ytrans;
        ccm = sort(unique(mvz(:)));
        tx = zeros(0, 1);
        ty = zeros(0, 1);
        tz = zeros(0, 1);
        for lc = ccm(:)'
            zmi = find(mvz == lc);
            zmc = tbase_opts.zlub(tbase_opts.zlui == lc);
            for zc = zmc(:)'
                tx = vertcat(tx, mvx(zmi));
                ty = vertcat(ty, mvy(zmi));
                tz = vertcat(tz, zc * ones(length(zmi), 1));
            end
        end
        rstring = [tx,ty,tz];

    % invalid type
    otherwise
        rstring = '* (unknown rtype in request!)';
end

% add second output argument if requested
if nargout > 1
    if ~isempty(rxlabelc)
        xlabel = sprintf('l_%d_%d_%d_%d_%d',rxlabelc);
    end
    if nargout > 2
        if ~isempty(rxlabelc)
            xlabelc = rxlabelc;
        end
    end
end

⌨️ 快捷键说明

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