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

📄 bvqxfile.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
📖 第 1 页 / 共 3 页
字号:
            if nargin > 2 && ...
                isstruct(varargin{3}) && ...
                length(fieldnames(varargin{3})) == 1 && ...
                all(strcmp(fieldnames(varargin{3}), {'L'}))
            
                % create object
                try
                    varargout{1} = class(varargin{3}, 'BVQXfile');
                catch
                    error( ...
                        'BVQXfile:BadStructForClass', ...
                        'Bad struct given, cannot create object.' ...
                    );
                end
            else
                error( ...
                    'BVQXfile:BadStructForClass', ...
                    'Bad struct given, cannot create object.' ...
                );
            end
            
        % list of methods per type
        case {'methods'}
            varargout{1} = bvqxfile_singleton.ff_methods;
            
        % evaluate NewFileCode
        case {'newcont'}
            if nargin < 3 || ...
               ~ischar(varargin{3}) || ...
                isempty(varargin{3}) || ...
               ~isfield(bvqxfile_singleton.ext, lower(varargin{3}(:)'))
                error( ...
                    'BVQXfile:BadArgument', ...
                    'Unknown filetype.' ...
                );
            end
            fftype = lower(varargin{3});
            ffspec = bvqxfile_singleton.ext.(fftype);
            xfft = ffspec{1}(end-2:end);
            spec = bvqxfile_singleton.file_formats.(xfft)(ffspec{2});
            bc = struct;
            newcode = strrep(strrep(spec.NewFileCode, '@', 'bc.'), '@@', 'bc');
            try
                eval(newcode);
                varargout{1} = bc;
                return;
            catch
                error( ...
                    'BVQXfile:EvaluationError', ...
                    'Couldn''t evaluate NewFileCode snippet for type %s.', ...
                    fftype ...
                );
            end
            
        % get object from lookup
        case {'object'}
            if nargin < 2 || ...
               ~isa(varargin{3}, 'double') || ...
                numel(varargin{3}) ~= 1 || ...
               ~any(bvqxclup == varargin{3})
                error( ...
                    'BVQXfile:LookupError', ...
                    'Object with given tag does not exist.' ...
                );
            end
            varargout{1} = BVQXfile(0, 'makeobject', struct('L', varargin{3}));
            varargout{2} = false;
            
        % list of objects (copy of global struct)
        case {'objects'}
            varargout{1} = bvqxcont;
            
        % transio size settings
        case {'transiosize'}
            
            % make per type setting
            if nargin > 3 && ...
                ischar(varargin{3}) && ...
                strcmp(makelabel(varargin{3}(:)'), varargin{3}(:)') && ...
                isfield(bvqxfile_singleton.ext, varargin{3}(:)') && ...
               ~isempty(strfind(bvqxfile_singleton.ext.(varargin{3}(:)'){1}, '.bff')) && ...
                isa(varargin{4}, 'double') && ...
                numel(varargin{4}) == 1 && ...
                ~isnan(varargin{4}) && ...
                varargin{4} > 4095
            
                % use a fixed size
                tsz = fix(varargin{4});
                fpos = bvqxfile_singleton.ext.(varargin{3}(:)'){2};
                bvqxfile_singleton.bff(fpos).TransIOSize = tsz;
                bvqxfile_singleton.file_formats.bff(fpos).TransIOSize = tsz;
                
                % store cache
                if nargin > 4 && ...
                    isa(varargin{5}, 'logical') && ...
                    numel(varargin{5}) == 1 && ...
                    varargin{5}
                    BVQXfile(0, 'updatecache');
                end
                
            % get per type setting
            elseif nargin > 2 && ...
                ischar(varargin{3}) && ...
                strcmp(makelabel(varargin{3}(:)'), varargin{3}(:)') && ...
                isfield(bvqxfile_singleton.ext, varargin{3}(:)') && ...
               ~isempty(strfind(bvqxfile_singleton.ext.(varargin{3}(:)'){1}, '.bff'))
                fpos = bvqxfile_singleton.ext.(varargin{3}(:)'){2};
                varargout{1} = bvqxfile_singleton.bff(fpos).TransIOSize;
                
            % set entire list of types
            elseif nargin > 2 && ...
                isstruct(varargin{3}) && ...
                numel(varargin{3}) == 1 && ...
                numel(fieldnames(varargin{3})) == numel(bvqxfile_singleton.bff)
            
                % get struct and check fieldnames against extensions
                tsz = varargin{3};
                tszf = fieldnames(tsz);
                for sc = 1:numel(bvqxfile_singleton.bff)
                    if ~strcmp(tszf{sc}, bvqxfile_singleton.bff(sc).Extensions{1}) || ...
                       ~isa(tsz.(tszf{sc}), 'double') || ...
                        numel(tsz.(tszf{sc})) ~= 1 || ...
                        isnan(tsz.(tszf{sc})) || ...
                        tsz.(tszf{sc}) < 4096
                        error( ...
                            'BVQXfile:BadArgument', ...
                            'Invalid transiosize structure.' ...
                        );
                    end
                end
                
                % make setting
                for sc = 1:numel(bvqxfile_singleton.bff)
                    bvqxfile_singleton.bff(sc).TransIOSize = tsz.(tszf{sc});
                    bvqxfile_singleton.file_formats.bff(sc).TransIOSize = tsz.(tszf{sc});
                end
                
                % store in cache
                if nargin > 3 && ...
                    isa(varargin{4}, 'logical') && ...
                    numel(varargin{4}) == 1 && ...
                    varargin{4}
                    BVQXfile(0, 'updatecache');
                end
                
            % set for all types
            elseif nargin > 2 && ...
                isa(varargin{3}, 'double') && ...
                numel(varargin{3}) == 1 && ...
                ~isnan(varargin{3}) && ...
                varargin{3} > 4095
                tsz = fix(varargin{3});
                
                % iterate over formats
                for sc = 1:numel(bvqxfile_singleton.bff)
                    bvqxfile_singleton.bff(sc).TransIOSize = tsz;
                    bvqxfile_singleton.file_formats.bff(sc).TransIOSize = tsz;
                end
                
                % store in cache
                if nargin > 3 && ...
                    isa(varargin{4}, 'logical') && ...
                    numel(varargin{4}) == 1 && ...
                    varargin{4}
                    BVQXfile(0, 'updatecache');
                end
                
            % get entire list of types
            elseif nargin == 2
                tsz = struct;
                for sc = 1:numel(bvqxfile_singleton.bff)
                    tsz.(bvqxfile_singleton.bff(sc).Extensions{1}) = ...
                        bvqxfile_singleton.bff(sc).TransIOSize;
                end
                varargout{1} = tsz;
            end
            
        % unwind stack
        case {'unwindstack'}
            
            % doesn't work if not enabled
            if ~bvqxfile_config.unwindstack
                return;
            end
            
            % get current stack
            cst = mystack;

            % iterate over all objects
            uobjs = false(1, numel(bvqxcont));
            for uc = 1:numel(uobjs)

                % continue if stack is empty
                if isempty(bvqxcont(uc).U)
                    continue;

                % on smaller stack -> unwind
                elseif numel(cst) < numel(bvqxcont(uc).U)
                    uobjs(uc) = true;

                % otherwise check stack of objects
                else

                    % iterate over stacks
                    ost = bvqxcont(uc).U;
                    for c = numel(ost):-1:1

                        % functions don't match -> unwind
                        if ~strcmp(ost{end + 1 - c}, cst{end + 1 - c})
                            uobjs(uc) = true;
                            break;
                        end
                    end
                end
            end

            % remove unwinded objects
            bvqxclup(uobjs) = [];
            bvqxcont(uobjs) = [];
            
        % update cache
        case {'updatecache'}
            
            % only works if cachefile set and enabled
            if isfield(bvqxfile_singleton, 'cachefile') && ...
                bvqxfile_singleton.use_info_cache
            
                % compile cache
                cache = struct;
                cache.version = bvqxfile_singleton.version;
                cache.file_formats = bvqxfile_singleton.file_formats;
                cache.ff_methods = bvqxfile_singleton.ff_methods;
                cache.bvqxconfig.type = bvqxfile_config.type;
                cache.bvqxconfig.update = bvqxfile_config.update;
                
                % save according to version
                if bvqxfile_singleton.mmversion < 7
                    save(bvqxfile_singleton.cachefile, 'cache');
                else
                    save(bvqxfile_singleton.cachefile, 'cache', '-v6');
                end
            end
            
        % disable obj_Update calls
        case {'updatedisable'}
            
            % for one type
            if nargin > 2 && ...
               ischar(varargin{3}) && ...
               isfield(bvqxfile_config.update, lower(varargin{3}(:)'))
                bvqxfile_config.update.(lower(varargin{3}(:)')) = false;
                
            % for all types
            elseif nargin == 2
                ftn = fieldnames(bvqxfile_config.update);
                for ftc = 1:numel(ftn)
                    bvqxfile_config.update.(ftn{ftc}) = false;
                end
            end
            
        % enable obj_Update calls
        case {'updateenable'}
            
            % for one type
            if nargin > 2 && ...
                ischar(varargin{3}) && ...
                isfield(bvqxfile_config.update, lower(varargin{3}(:)'))
                bvqxfile_config.update.(lower(varargin{3}(:)')) = true;
                
            % for all types
            elseif nargin == 2
                ftn = fieldnames(bvqxfile_config.update);
                for ftc = 1:numel(ftn)
                    bvqxfile_config.update.(ftn{ftc}) = true;
                end
            end
            
        % state of update flag
        case {'updatestate'}
            
            % for one type
            if nargin > 2 && ...
                ischar(varargin{3}) && ...
                isfield(bvqxfile_config.update, lower(varargin{3}(:)'))
            
                % set flag
                if nargin > 3 && ...
                    islogical(varargin{4}) && ...
                   ~isempty(varargin{4})
                    bvqxfile_config.update.(lower(varargin{3}(:)')) = varargin{4}(1);
                
                % get flag
                else
                    varargout{1} = bvqxfile_config.update.(lower(varargin{3}(:)'));
                end
           
           
            % for all types
            else
                
                % set flags
                if nargin > 2 && ...
                    isstruct(varargin{3}) && ...
                    numel(varargin{3}) == 1 && ...
                    numel(fieldnames(varargin{3})) == numel(fieldnames(bvqxfile_config.update)) && ...
                    all(strcmp(fieldnames(varargin{3}), fieldnames(bvqxfile_config.update)))
                
                    % check struct
                    sf = fieldnames(varargin{3});
                    for sfc = 1:numel(sf)
                        if ~islogical(varargin{3}.(sf{sfc})) || ...
                            numel(varargin{3}.(sf{sfc})) ~= 1
                            error( ...
                                'BVQXfile:BadArgument', ...
                                'Invalid updatestate argument.' ...
                            );
                        end
                    end
                    
                    % set struct
                    bvqxfile_config.update = varargin{3};
                
                % get flags
                else
                    
                    % pass out update struct
                    varargout{1} = bvqxfile_config.update;
                end
            end
            
        % bail out on study commands
        otherwise
            error( ...
                'BVQXtools:BVQXfile:BadSpecialArgument', ...
                'Invalid special argument given.' ...
            );
    end

% just a lookup value
elseif nargin == 1 && ...
    isa(varargin{1}, 'double') && ...
    numel(varargin{1}) == 1 && ...
    any(bvqxclup == varargin{1})

    % return object
    varargout{1} = BVQXfile(0, 'makeobject', struct('L', varargin{1}));
    
% make case for NULL arguments (return ROOT if needed)
elseif nargin == 0
    if nargout > 0
        varargout{1} = BVQXfile(0, 'makeobject', struct('L', 1));
    end

% else
else
    if nargin == 1 && ...
        ischar(varargin{1})
        error( ...
            'BVQXtools:BVQXfile:BadArgument', ...
            'File not found.' ...
        );
    else
        error( ...
            'BVQXtools:BVQXfile:BadArgument', ...
            'Bad argument combination or file not writable.' ...
        );
    end

% end of argument test if
end



% internal function for stack unwinding
function mst = mystack
    
    % persistent config
    persistent msc;
    if isempty(msc)
        mlv = version;
        msc = (str2double(mlv(1)) < 7);
    end
    
    % which version
    if msc
        mst = dbstack;
        mst = {mst.name};
    else
        mst = dbstack('-completenames');
        mst = {mst.file};
    end
    
    % remove BVQXfile from stack
    mst(1:2) = [];
    rst = true(1, numel(mst));
    for rc = 1:numel(rst)
        if ~isempty(strfind(mst{rc}, '@BVQXfile'))
            rst(rc) = false;
        end
    end
    mst = mst(rst);
% end of function mst = mystack

⌨️ 快捷键说明

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