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

📄 dcm_value.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function dcmval = dcm_Value(hfile, vkey, varargin)
% DCM::Value  - return a tag's value
%
% FORMAT:       [dcmval] = dcm.Value(key [, ...]);
%
% Input fields:
%
%       key         DICOM key (0008.0010) or tag (PatientsName)
%       ...         default value if not found
%
% Output fields:
%
%       dcmval      value for given key/tag

% Version:  v0.7b
% Build:    7083009
% Date:     Aug-30 2007, 9:00 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% persistent dictionaries
persistent dcmv_dicts
if isempty(dcmv_dicts)
    dcmv_dicts = struct;
    
    % load standard dict
    dcmv_dicts.OFFIS = dicom_dic;
end

% only valid for single file
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'dcm')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% dict
bc = bvqxfile_getcont(hfile.L);
dict = bc.DataDictionary;

% check key
if ischar(vkey)
    vkey = vkey(:)';
    if length(vkey) == 9 && ...
       ~isempty(regexpi(vkey, '^[0-9a-f]{4}[^0-9a-f][0-9a-f]{4}$'))
        vkey = vkey([1:4,6:9]);
    end
    if length(vkey) == 8 && ...
       ~isempty(regexpi(vkey, '^[0-9a-f]+$')) && ...
       ~all(double(vkey) > 64)
        vkey = ['k_' upper(vkey(1:4)) '_' upper(vkey(5:8))];
    elseif ~strcmp(vkey, makelabel(vkey))
        error( ...
            'BVQXfile:BadArgument', ...
            'Invalid DICOM tag number/key given.' ...
        );
    else
        if isempty(dict)
            warning( ...
                'BVQXfile:InternalWarning', ...
                'Data dictionary not looked up.' ...
            );
            dcm_DetectDictionary(hfile, 'auto');
            dict = bc.DataDictionary;
        end
        if ~isfield(dcmv_dicts, dict)
            error( ...
                'BVQXfile:BadSetting', ...
                'Unknown DICOM dictionary set.' ...
            );
        end
        dict = dcmv_dicts.(dict);
        if ~isfield(dict, vkey)
            error( ...
                'BVQXfile:BadArgument', ...
                'Unknown DICOM tag key given.' ...
            );
        end
        vkey = dict.(vkey);
        if isfield(bc.DataKeyLookup, vkey)
            dcmval = bc.Data(bc.DataKeyLookup.(vkey)).Value;
        elseif isfield(bc.MetaKeyLookup, vkey)
            dcmval = bc.Meta(bc.MetaKeyLookup.(vkey)).Value;
        elseif nargin > 2
            dcmval = varargin{1};
        else
            error( ...
                'BVQXfile:BadArgument', ...
                'Given DICOM tag key not present in file.' ...
            );
        end
        dcmval = interpret_dcmval(dcmval);
        return;
    end
    if ~isfield(bc.DataKeyLookup, vkey)
        if nargin < 3
            error( ...
                'BVQXfile:BadArgument', ...
                'Given DICOM tag not present in file.' ...
            );
        else
            dcmval = varargin{1};
            dcmval = interpret_dcmval(dcmval);
            return;
        end
    end
    dcmval = bc.Data(bc.DataKeyLookup.(vkey)).Value;
    dcmval = interpret_dcmval(dcmval);

% numeric key format
elseif isa(vkey, 'double')
    if numel(vkey) == 2 && ...
       ~any(isinf(vkey) | isnan(vkey) | vkey < 0 | vkey > 65535)
        try
            dcmval = dcm_Value(hfile, sprintf('%04x%04x', vkey(1), vkey(2)), varargin{:});
        catch
            error( ...
                'BVQXfile:BadArgument', ...
                'Given DICOM tag not present in file.' ...
            );
        end
    elseif numel(vkey) == 1 && ...
        nargin > 2 && ...
        isa(varargin{1}, 'double') && ...
       ~isinf(vkey) && ...
       ~isnan(vkey) && ...
        vkey >= 0 && ...
        vkey < 65536 && ...
        numel(varargin{1}) == 1 && ...
       ~isinf(varargin{1}) && ...
       ~isnan(varargin{1}) && ...
        varargin{1} >= 0 && ...
        varargin{1} < 65536
        try
            dcmval = dcm_Value(hfile, sprintf('%04x%04x', vkey, varargin{1}), varargin{2:end});
        catch
            error( ...
                'BVQXfile:BadArgument', ...
                'Given DICOM tag not present in file.' ...
            );
        end
    else
        error( ...
            'BVQXfile:BadArgument', ...
            'Invalid DICOM tag (or key) given.' ...
        );
    end
    dcmval = interpret_dcmval(dcmval);
    
else
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid DICOM tag (or key) given.' ...
    );
end


% sub function
function dcmval = interpret_dcmval(dcmval)
    if ischar(dcmval) && ...
       ~isempty(dcmval)
        dcmval = dcmval(:)';
        if ~isempty(regexpi(dcmval, ...
                '^\s*[\+\-]?\d+(\.\d+)?(\s*\\\s*[\+\-]?\d+(\.\d+)?)*\s*$'))
            dcmvalc = splittocell(dcmval, '\');
            dcmvaln = zeros(1, numel(dcmvalc));
            try
                for vc = 1:numel(dcmvalc)
                    dcmvaln(vc) = str2double(dcmvalc{vc});
                end
            catch
                return;
            end
            dcmval = dcmvaln;
        end
    elseif isnumeric(dcmval)
        dcmval = double(dcmval);
    end
% end of function dcmval = interpret_dcmval(dcmval)

⌨️ 快捷键说明

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