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

📄 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 页
字号:
        return;
    
    % patch extension only with "new:???"
    elseif numel(filename) < 5 && ...
       (exist(filename, 'file') ~= 2 || ...
        exist([filename '.m'], 'file') == 2)
        filename = ['new:' filename];
        
    end
    
    % checking for 'new:???'
    if length(filename) > 5 && ...
        strcmpi(filename(1:4), 'new:') && ...
       ~any(filename == '\' | filename == '/' | filename == '.') && ...
        isfield(ext, lower(filename(5:end)))

        % get spec
        fftype = lower(filename(5:end));
        ffspec = ext.(fftype);
        xfft = ffspec{1}(end-2:end);
        spec = bvqxfile_singleton.file_formats.(xfft)(ffspec{2});

        % no "new" code is available
        if isempty(spec.NewFileCode)
            error( ...
                'BVQXfile:IncompleteSpec', ...
                'For %s type files, no NewFileCode is available.', ...
                fftype ...
            );
        end;

        % make new object's lookup value
        nlup = rand(1, 1);
        while any(bvqxclup == nlup)
            nlup = rand(1, 1);
        end

        % get new object's content
        try
            bc = BVQXfile(0, 'newcont', fftype);
            if bvqxfile_config.unwindstack
                mst = mystack;
            else
                mst = {};
            end
            bvqxcont(end + 1) = struct( ...
                'C', bc, ...
                'F', '', ...
                'L', nlup, ...
                'S', spec, ...
                'U', {mst});
            bvqxclup(end + 1) = nlup;
        catch
            error( ...
                'BVQXfile:EvaluationError', ...
                'Couldn''t evaluate NewFileCode snippet for type %s.', ...
                fftype ...
            );
        end

        % build object
        varargout{1} = BVQXfile(0, 'makeobject', struct('L', nlup));
        varargout{2} = true;
        return;
    end

    % make absolute!
    [isabs{1:2}] = isabsolute(filename);
    filename = isabs{2};

    % last time check file (on absolute path)
    if exist(filename, 'file') ~= 2

        % bail out if non existant
        error( ...
            'BVQXtools:BVQXfile:FileNotExists', ...
            'The filename given does not exist: ''%s''.', ...
            varargin{1}(:)' ...
        );
    end

    % get file name parts
    [fx{1:3}] = fileparts(filename);
    fx = fx{3};
    if ...
       ~isempty(fx) && ...
        fx(1) == '.'
        fx(1) = [];
    end

    % any extension based match
    exf = strcmpi(fx, exn);
    if any(exf)

        % get match and matching extension field name
        exf = find(exf);
        exn = exn{exf(1)};

        % look up in extensions
        if isfield(ext, exn)
            ffspec = ext.(exn);
            xfft = ffspec{1}(end-2:end);
            ff = bvqxfile_singleton.file_formats.(xfft)(ffspec{2});

        % or error !
        else
            error( ...
                'BVQXtools:BVQXfile:BadExtension', ...
                'Extension given, but not in BFF/TFF list: %s.', ...
                exn ...
            );
        end

        % match found
        exf = true;
    else

        % no match found
        exf = false;
    end

    % not yet identified, try magic
    if ~exf
        maf = false;
        try
            detmag = bvqxfile_detectmagic(filename, bvqxfile_singleton.mag);
        catch
            detmag = '';
        end
        if ~isempty(detmag)

            % either bff
            if isfield(ext, detmag)
                ffspec = ext.(detmag);
                xfft = ffspec{1}(end-2:end);
                ff = bvqxfile_singleton.file_formats.(xfft)(ffspec{2});

            % or error !
            else
                error( ...
                    'BVQXtools:BVQXfile:BadExtension', ...
                    'Magic found, but type not in BFF/TFF list: %s.', ...
                    detmag ...
                );
            end
            maf = true;
        end
    end

    if ~exf && ~maf
        error( ...
            'BVQXtools:BVQXfile:BadFileContent', ...
            'Unknown file type. Cannot read ''%s''.', ...
            filename ...
        );
    end

    % read file
    try
        switch lower(ff.FFTYPE)
            case {'bff'}
                ffcont = bffio(filename, ff);
            case {'tff'}
                ffcont = tffio(filename, ff);
            otherwise
                error( ...
                    'BVQXtools:BVQXfile:BadFFTYPE', ...
                    'FF type %s not supported yet.', ...
                    ff.FFTYPE ...
                );
        end
        if ispc
            filename = strrep(filename, '\', '/');
        end
        nlup = rand(1, 1);
        while any(bvqxclup == nlup)
            nlup = rand(1, 1);
        end
        if bvqxfile_config.unwindstack
            mst = mystack;
        else
            mst = {};
        end
        bvqxcont(end + 1) = struct( ...
            'C', ffcont, ...
            'F', filename, ...
            'L', nlup, ...
            'S', ff, ...
            'U', {mst});
        bvqxclup(end + 1) = nlup;
        varargout{1} = BVQXfile(0, 'makeobject', struct('L', nlup));
        varargout{2} = true;
        return;
    catch
        error( ...
            'BVQXtools:BVQXfile:BFFioFailed', ...
            'Error calling bffio(...): ''%s''.', ...
            lasterr ...
        );
    end


% elseif ... other input argument combinations
elseif ...
    nargin == 2 && ...
    ischar(varargin{1}) && ...
   ~isempty(varargin{1}) && ...
    numel(varargin{2}) == 1 && ...
    isBVQXfile(varargin{2}, true)

    % try writing file
    try
        olup = (bvqxclup == varargin{2}.L);
        ostr = bvqxcont(olup);
        switch lower(ostr.S.FFTYPE)
            case {'bff'}
                bvqxcont(olup).C = bffio(varargin{1}, ostr.S, ostr.C);
            case {'tff'}
                bvqxcont(olup).C = tffio(varargin{1}, ostr.S, ostr.C);
            otherwise
                error( ...
                    'BVQXtools:BVQXfile:BadFFTYPE', ...
                    'FF type %s not supported yet.', ...
                    ostr.S.FFTYPE ...
                );
        end
    catch
        error( ...
            'BVQXtools:BVQXfile:xFFioFailed', ...
            'Error calling ?ffio(...): ''%s''.', ...
            lasterr ...
        );
    end

% special cases for internal call
elseif nargin > 1 && ...
    isa(varargin{1}, 'double') && ...
    numel(varargin{1}) == 1 && ...
    varargin{1} == 0 && ...
    ischar(varargin{2}) && ...
    strcmp(makelabel(varargin{2}(:)'), varargin{2}(:)')

    % what special case
    switch (lower(varargin{2}(:)'))
        
        % clear storage completely
        case {'clearallobjects'}
            
            % simply remake internal arrays
            bvqxfile_singleton(1) = [];
            BVQXfile;

        % remove one object from the array
        case {'clearobj'}
            if nargin > 2 && ...
                isa(varargin{3}, 'double') && ...
                all(varargin{3}(:) >= 0 & varargin{3}(:) <= 1)
            
                % remove
                bvqxfile_clear(varargin{3}(:)');
            end
            
        % class (types) configuration
        case {'config'}
            if nargin < 3 || ...
               ~ischar(varargin{3}) || ...
              (~isfield(bvqxfile_config.type, varargin{3}(:)') && ...
               ~isfield(bvqxfile_config, varargin{3}(:)'))
                error( ...
                    'BVQXfile:BadArgument', ...
                    'Bad or missing argument in config call.' ...
                );
            end
            
            % global configuration
            if isfield(bvqxfile_config, varargin{3}(:)')
                
                % set
                if nargin > 3
                    bvqxfile_config.(varargin{3}(:)') = varargin{4};
                else
                    varargout{1} = bvqxfile_config.(varargin{3}(:)');
                end
                
            % type configuration
            else
                
                % get current config
                curcfg = bvqxfile_config.type.(varargin{3}(:)');
                
                % sub indexing
                if nargin > 3 && ...
                    ischar(varargin{4}) && ...
                   ~isempty(varargin{4}) && ...
                    isfield(curcfg, varargin{4}(:)')
                
                    % set
                    if nargin > 4
                        bvqxfile_config.type.(varargin{3}(:)').(varargin{4}(:)') = varargin{5};
                        
                    % get
                    else
                        varargout{1} = curcfg.(varargin{4}(:)');
                    end
                    
                % entire type config
                else
                    varargout{1} = curcfg;
                end
            end
            
        % copy object
        case {'copyobject'}
            if nargin > 2 && ...
                isa(varargin{3}, 'double') && ...
                numel(varargin{3}) == 1 && ...
                ~isnan(varargin{3}) && ...
                varargin{3} < 1 && ...
                any(bvqxclup == varargin{3})
                olup = find(bvqxclup == varargin{3});
                nlup = rand(1, 1);
                while any(bvqxclup == nlup)
                    nlup = rand(1, 1);
                end
                bvqxclup(end + 1) = nlup;
                bvqxcont(end + 1) = bvqxcont(olup(1));
                bvqxcont(end).F = '';
                bvqxcont(end).L = nlup;
                if bvqxfile_config.unwindstack
                    bvqxcont(end).U = mystack;
                end
                varargout{1} = BVQXfile(0, 'makeobject', struct('L', nlup));
                varargout{2} = true;
            else
                error( ...
                    'BVQXfile:BadArgument', ...
                    'Bad argument or invalid object lookup.' ...
                );
            end
            
        % list of supported extensions / types
        case {'extensions'}
            varargout{1} = bvqxfile_singleton.ext;
            
        % format specifications
        case {'formats'}
            varargout{1} = bvqxfile_singleton.file_formats;
            
        % check object validity
        case {'isobject'}
            
            % class check first
            if ~isa(varargin{3}, 'BVQXfile')
                varargout{1} = false;
                return;
            end
            hfile = varargin{3};
            
            % arguments
            if nargin > 3 && ...
                islogical(varargin{4}) && ...
                ~isempty(varargin{4}) && ...
                varargin{4}(1)
                if nargin > 4 && ...
                    ischar(varargin{5})
                    chtypest = varargin{5}(:)';
                else
                    chtypest = '';
                end
            else
                varargout{1} = true(size(hfile));
                return;
            end
                
            % validation ?
            isf = true(size(hfile));
            for oc = 1:numel(hfile)

                % L must lead to global storage
                if ~any(bvqxclup == hfile(oc).L)
                    isf(oc) = false;
                    continue;
                end

                % if that passed, check extension if given
                if ~isempty(chtypest) && ...
                   ~strcmpi(chtypest, bvqxcont(bvqxclup == hfile(oc).L).S.Extensions{1})
                    isf(oc) = false;
                end
            end
            varargout{1} = isf;

        % magic detection tokens
        case {'magic'}
            varargout{1} = bvqxfile_singleton.mag;
            
        % create object from struct
        case {'makeobject'}
            
            % unwind stack first
            if bvqxfile_config.unwindstack
                BVQXfile(0, 'unwindstack');
            end
            
            % check integrity

⌨️ 快捷键说明

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