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

📄 checkstruct.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function cstr = checkstruct(cstr, varargin)
% checkstruct  - check an 1x1 struct for validity
%
% FORMAT:       checked = checkstruct(tocheck, options [, remunknown])
%
% Input fields:
%
%       tocheck     1x1 struct to check
%       options     Nx4 cell array with options, whereas for each row
%                   has the form or {field, classname, condition, default}, and
%                   the first cell contains the field name
%                   the second cell is one out of
%                   {'char', 'numeric', 'struct', 'cell'}
%                   the third cell is one out of
%                   {'nonempty', 'noinfnan', 'label', 'expression', 'deblank'}
%                   and the last cell is the default for when either the
%                   type or condition doesn't match
%       remunknown  if given removes all unnamed fields from tocheck
%
% Output fields:
%
%        checked    struct after type checking

% Version:  v0.6e
% Build:    7041710
% Date:     Apr-17 2007, 10:02 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% basic argument check
if nargin < 2 || ...
  ~iscell(varargin{1}) || ...
   isempty(varargin{1}) || ...
   size(varargin{1},2) ~= 4
    error( ...
        'BVQXtools:BadArgument', ...
        'Checkstruct needs a Mx4 options argument.' ...
    );
end
opt = varargin{1};

% build struct from non structs
if ~isstruct(cstr) || ...
    length(cstr) ~= 1
    cstr = struct;
end

% remove unknown fields
if nargin > 2
    remunknown = true;
    flist      = struct;
else
    remunknown = false;
end

% grand try block for bad sub-types
try

    % iterate over fields
    for fc = 1:size(opt,1)
        tname = opt{fc,1}(:)';

        % keep track of fieldnames
        if remunknown
            flist.(tname) = true;
        end

        % get default content
        tdef = opt{fc,4};

        % check field existance
        if ~isfield(cstr, tname)
            cstr.(tname) = tdef;
            continue;
        end

        % get current content
        tcont = cstr.(tname);

        % check field class
        tclass = lower(opt{fc,2}(:)');
        if isempty(tclass)
            continue;
        end
        tclcmp = lower(class(tcont));
        if length(tclass) ~= length(tclcmp) || ...
            any(tclass ~= tclcmp)
            cstr.(tname) = tdef;
            continue;
        end

        % options
        topt = opt{fc,3}(:)';

        % character option
        if ischar(topt)

            % deblanking
            switch (lower(opt{fc,3}(:)')), case {'deblank'}

                if ischar(tcont)
                    cstr.(tname) = deblank(tcont);
                end

            % expression
            case {'expression'}

                if ~ischar(tcont) || ...
                   ~isempty(checksyntax(tcont(:)'))
                    cstr.(tname) = tdef;
                else
                    cstr.(tname) = deblank(tcont(:)');
                end

            % label
            case {'label'}

                if ~isrealvarname(deblank(tcont(:)'))
                    cstr.(tname) = tdef;
                else
                    cstr.(tname) = deblank(tcont(:)');
                end

            % nonempty
            case {'nonempty'}

                if isempty(cstr.(tname))
                    cstr.(tname) = tdef;
                end

            % noinfnan
            case {'noinfnan'}

                if isnumeric(tcont) && ...
                   (any(isinf(tcont)) || any(isnan(tcont)))
                    cstr.(tname) = tdef;
                end

            % singular content
            case {'singular'}

                if numel(cstr.(tname)) ~= 1
                    cstr.(tname) = tdef;
                end

            end

        % cell option
        elseif iscell(topt)

            % test content
            cmatch = false;
            tsize  = numel(tcont);
            for cc = 1:length(topt)
                if length(topt{cc}) == tsize && ...
                    all(topt{cc} == tcont)
                    cmatch = true;
                    break;
                end
            end

            % match not found
            if ~cmatch
                cstr.(tname) = tdef;
            end
        end
    end

catch
    error( ...
        'BVQXtools:BadArgument', ...
        'A suboption of options seems invalid (%s).', ...
        lasterr ...
    );
end

⌨️ 快捷键说明

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