hasfields.m

来自「toolbox of BVQX, This is the access betw」· M 代码 · 共 56 行

M
56
字号
function bvalue = hasfields(teststruct, varargin)
% hasfields  - test whether a struct has the same fields as a template
%
% FORMAT:       boolval = hasfields(teststruct, template [, onlythese])
%
% Input fields:
%
%       teststruct  struct array that is tested
%       template    template struct that will be used
%       onlythese   optional argument, if given only returns true if no
%                   other fields are presents in teststruct
%
% Note: the order of the fieldnames is irrelevant for the test!
%
% See also ISFIELD.

% Version:  v0.5c
% Build:    6120415
% Date:     Dec-04 2006, 3:15 PM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
   ~isstruct(teststruct) || ...
   ~isstruct(varargin{1})
    error( ...
        'BVQXtools:BadArgument', ...
        'Too few or bad input arguments given.' ...
    );
end
template = varargin{1};

% init return value
bvalue = false;

% get fieldnames
does = fieldnames(teststruct);
must = fieldnames(template);

% return false if number of fields mismatch
if nargin > 2 && ...
   length(does) ~= length(must)
    return;
end

% test every must-fields
for fc = 1:length(must)
    if ~any(strcmp(does,must{fc}))
        return;
    end
end

% all checks passed
bvalue = true;

⌨️ 快捷键说明

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