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

📄 bvqxinifile.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
📖 第 1 页 / 共 5 页
字号:
                    varargin{4}(:)', ...
                    cfile.filename ...
                );
            end

            % found -> return
            varargout{1} = sfound;

        % direct hit
        else
            varargout{1} = csection.(varargin{4}(:)');
        end


    % is given argument an existing section ?
    case {'issection'}

        % check for section field in call
        if nargin < 3 || ...
           ~ischar(varargin{3}) || ...
            isempty(varargin{3})
            error( ...
                'BVQXinifile:CallingConvention', ...
                'IsSection needs a CHAR section name in call.' ...
            );
        end

        % get info...
        varargout{1} = isfield(cfile.icontent, varargin{3}(:)');


    % are given arguments an existing section/setting pair ?
    case {'issetting'}

        % check for section and setting fields in call
        if nargin < 4 || ...
           ~ischar(varargin{3}) || ...
           ~ischar(varargin{4}) || ...
            isempty(varargin{3}) || ...
            isempty(varargin{4})
            error( ...
                'BVQXinifile:CallingConvention', ...
                'IsSetting needs two CHAR arguments in call.' ...
            );
        end

        % init output variable
        varargout{1} = false;

        % only further checks if section exists
        if isfield(cfile.icontent, varargin{3}(:)')
            
            % get test section
            tsection = cfile.icontent.(varargin{3}(:)');
            if ~isstruct(tsection)
                varargout{1} = true;
                return;
            end

            % reset output variable
            varargout{1} = isfield(tsection, varargin{4}(:)');

            % if exists or no parent should be inspected then return
            if varargout{1}
                return;
            end
        end

        % if we have a parent and should inspect them
        if ~isempty(cfile.parent) && ...
           (nargin < 5 || ...
            ~islogical(varargin{5}) || ...
            isempty(varargin{5}) || ...
            ~varargin{5}(1))
            sfound = false;
            tfid = cfid;

            % iterate until found or more parent
            while ~sfound && ...
               ~isempty(bvqxinic(tfid).parent)

                % lookup parent
                tfid = find(bvqxinif.matrix == bvqxinic(tfid).parent(1));

                % bail out if gone
                if isempty(tfid)
                    error( ...
                        'BVQXinifile:InvalidFileId', ...
                        'Parent object disappeared, memory glitch?' ...
                    );
                end

                % check for field
                if isfield(bvqxinic(tfid).icontent, varargin{3}(:)')
                    tsection = bvqxinic(tfid).icontent.(varargin{3}(:)');
                    if ~isstruct(tsection)
                        if ~iscell(tsection)
                            tsection = struct('Value', tsection);
                        else
                            tsection = struct('Value', {tsection});
                        end
                    end
                else
                    tsection = struct;
                end
                
                if isfield(tsection, varargin{4}(:)')
                    sfound = true;
                end
            end
            varargout{1} = sfound;
        end


    % get validity flag
    case {'isvalid'}
        varargout{1} = true;


    % read IniFile from disk
    case {'loadinifile'}

        % do we have a valid filename
        if nargin < 3 || ...
           ~ischar(varargin{3}) || ...
            isempty(varargin{3}) || ...
            exist(varargin{3}(:)', 'file') ~= 2
            error( ...
                'BVQXinifile:CallingConvention', ...
                'LoadIniFile needs a valid filename in call.' ...
            );
        end

        % only works for the factory object
        if cfid ~= 0
            error( ...
                'BVQXinifile:CallingConvention', ...
                'LoadIniFile only works for the factory object.' ...
            );
        end

        % get filename, and make sure to have absolute path
        [isabs{1:2}] = isabsolute(varargin{3}(:)');
        filename = isabs{2};

        % try to read file
        try
            filecont = asciiread(filename);
        catch
            error( ...
                'BVQXinifile:FileNotReadable', ...
                'The specified file is not readable.' ...
            );
        end

        % conversion activated?
        iconvert = 0;
        if nargin > 3
            sconvert = varargin{4}(:)';
            if isnumeric(sconvert) && ...
                numel(sconvert) == 1
                if sconvert == 1
                    iconvert = 1;
                elseif sconvert == 2
                    iconvert = 2;
                end
            else
                iconvert = 1;
            end
        end

        % check for current files in matrix (re-open)
        for ofc = 1:length(bvqxinif.matrix)

            % do we already have this file opened?
            if strcmp(filename, bvqxinic(ofc).filename) || ...
               (ispc && ...
                strcmpi(filename, bvqxinic(ofc).filename))

                % then initialize output object
                varargout{1} = ...
                    class(struct('L', bvqxinif.matrix(ofc)), 'BVQXinifile');

                % conversion on top? -> reconvert now
                if iconvert > 0 && ...
                    bvqxinic(ofc).convert == 0

                    % get current content
                    ccontent = bvqxinic(ofc).icontent;
                    sectnames = fieldnames(ccontent);

                    % try to convert any strings with [...] or {...} content
                    try
                        % iterate over sections
                        for csect = 1:length(sectnames)

                            % get setting names
                            tsection = ccontent.(sectnames{csect});
                            if ~isstruct(tsection)
                                if ~iscell(tsection)
                                    tsection = struct('Value', tsection);
                                else
                                    tsection = struct('Value', {tsection});
                                end
                            end
                            ssettings = fieldnames(tsection);

                            % iterate over settings
                            for cset = 1:length(ssettings)

                                % get value
                                svalue = tsection.(ssettings{cset});

                                % if convertable
                                if ischar(svalue) && ...
                                   ~isempty(svalue) && ...
                                   ((svalue(1) == '[' && ...
                                     svalue(end) == ']') || ...
                                    (svalue(1) == '{' && ...
                                     svalue(end) == '}'))
                                    try
                                        tsection.(ssettings{cset}) = ...
                                            eval(strrep(svalue, ...
                                                 '$SELF.', 'ccontent.'));
                                    catch
                                        error('CONVERSION_FAILED');
                                    end
                                end
                            end
                            ccontent.(sectnames{csect}) = tsection;
                        end

                    % conversion failed, bail out
                    catch
                        error( ...
                            'BVQXinifile:ConversionFailed', ...
                            'Couldn''t convert already open file %s.', ...
                            bvqxinic(ofc).filename ...
                        );
                    end

                    % now set new conversion mode and content
                    bvqxinic(ofc).convert = iconvert;
                    bvqxinic(ofc).icontent = ccontent;
                end

                % do return
                return;
            end
        end

        % file not already open -> get unique object fid
        cfid = floor(2 ^ 31 * rand(1) + 1);
        while ~isempty(bvqxinif.matrix) && ...
           any(bvqxinif.matrix == cfid)
            cfid = floor(2 ^ 31 * rand(1) + 1);
        end

        % read file contents and cut trailing lines
        [icontent, prefile, postfile, newerrors] = i_parse( ...
            filecont, bvqxinif.fterm, iconvert);

        % errors during parse
        if ~isempty(newerrors)
            error( ...
                'BVQXinifile:IniParseError', ...
                'Parsing errors occurred:\n\n%s', ...
                gluetostring(newerrors, char(10)) ...
            );
        end

        % add new file to matrix
        bvqxinif.matrix(end + 1) = cfid;
        ifid = length(bvqxinif.matrix);
        bvqxinic(ifid).caller = extcaller(1);
        bvqxinic(ifid).children = [];
        bvqxinic(ifid).convert = iconvert;
        bvqxinic(ifid).filename = filename;
        bvqxinic(ifid).icontent = icontent;
        bvqxinic(ifid).parent = [];
        bvqxinic(ifid).postfile = postfile;
        bvqxinic(ifid).prefile = prefile;
        bvqxinic(ifid).readwrite = 'a';

        % set output object
        varargout{1} = class(struct('L', cfid),'BVQXinifile');


    % create IniFile object from scratch
    case {'newinifile'}

        % only works for factory object
        if cfid ~= 0
            error( ...
                'BVQXinifile:CallingConvention', ...
                'NewIniFile only works for the factory object.' ...
            );
        end

        % get unique object fid
        cfid = floor(2 ^ 31 * rand(1) + 1);
        while ~isempty(bvqxinif.matrix) && ...
            any(bvqxinif.matrix == cfid)
            cfid = floor(2 ^ 31 * rand(1) + 1);
        end

        % update matrix and init object fields
        bvqxinif.matrix(end + 1) = cfid;
        ifid = numel(bvqxinif.matrix);
        bvqxinic(ifid).caller = extcaller(1);
        bvqxinic(ifid).children = [];
        bvqxinic(ifid).convert = 0;
        bvqxinic(ifid).filename = '';
        bvqxinic(ifid).icontent = struct;
        bvqxinic(ifid).parent = [];
        bvqxinic(ifid).postfile = [bvqxinif.fterm bvqxinif.lineout];
        bvqxinic(ifid).prefile = '';
        bvqxinic(ifid).readwrite = 'a';

        % set output object values
        varargout{1} = class(struct('L', cfid), 'BVQXinifile');

        % conversion?
        if nargin > 2
            sconvert = varargin{3}(:)';

            % numeric convert -> [0..2]
            if isnumeric(sconvert) && ...
               ~numel(sconvert) == 1 && ...
               ~isnan(sconvert) && ...
               ~isinf(sconvert)
                bvqxinic(ifid).convert = min(2, max(0, floor(sconvert)));

            % for 'exact' set to 2
            elseif ischar(sconvert) && ...
                strcmpi(sconvert, 'exact')
                bvqxinic(ifid).convert = 2;

            % else to 1
            else
                bvqxinic(ifid).convert = 1;
            end
        end


    % parse IniFile from string
    case {'parseinistring'}

        % do we have a valid ini-string?
        if nargin < 3 || ...
           ~ischar(varargin{3}) || ...
            isempty(varargin{3})
            error( ...
                'BVQXinifile:CallingConvention', ...
                'ParseIniString needs a non-empty inistring to parse.' ...
            );
        end

        % only works for the factory object
        if cfid ~= 0
            error( ...
                'BVQXinifile:CallingConvention', ...
                'NewIniFile only works for the factory object.' ...
            );
        end

        % conversion activated?
        iconvert = 0;
        if nargin > 3
            sconvert = varargin{4}(:)';
            if isnumeric(sconvert) && ...
               numel(sconvert) == 1
                if strCmd.convert(1) == 1
                    iconvert = 1;
                elseif strCmd.convert(1) == 2
                    iconvert = 2;
                end
            elseif ischar(sconvert) && ...
                strcmpi(sconvert, 'exact')
                iconvert = 2;
            else
                iconvert = 1;
            end
        end

        % get unique object fid
        cfid = floor(2 ^ 31 * rand(1) + 1);
        while ~isempty(bvqxinif.matrix) && ...
            any(bvqxinif.matrix == cfid)
            cfid = floor(2 ^ 31 * rand(1) + 1);

⌨️ 快捷键说明

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