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

📄 bffio.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
📖 第 1 页 / 共 3 页
字号:
            end

        % end of a loop
        case {'eloop'}

            % check name of LAST ENTERED loop
            if ~strcmp(rexpr, loopx{end})
                error( ...
                    'BVQXtools:BadBFFSpec', ...
                    'Invalid LOOP end token found: ''%s''.', ...
                    rexpr ...
                );
            end

            % increase loop counter and check if the loop is done
            try
                leaveloop = false;
                eval([...
                    'namevars.' rexpr '=namevars.'  rexpr '+1;' ...
                    'if namevars.' rexpr '>loopi(loopc).dim,leaveloop=true;end']);
            catch
                error( ...
                    'BVQXtools:BadExpression', ...
                    'Couldn''t increase/check LOOP counter: ''%s''.', ...
                    rexpr ...
                );
            end

            % if we're NOT leaving loop
            if ~leaveloop

                % set next rule to process to first loop line
                rulec = loopi(loopc).firstrule;

            % if we're leaving loop
            else

                % pop loopi/x(loopc)
                loopi(loopc) = [];
                loopx(loopc) = [];
                loopc = loopc - 1;

            end

        % expression
        case {'expre'}

            % if no given condition is given
            if isempty(rcond)

                % simply execute expression
                try
                    eval(rexpr);
                catch
                    error( ...
                        'BVQXtools:BadExpression', ...
                        'Couldn''t evaluate EXPRE: ''%s''.', ...
                        rexpr ...
                    );
                end

            % with a given condition
            else

                % execute upon condition
                try
                    eval(['if ' rcond ',' rexpr ';end']);
                catch
                    error( ...
                        'BVQXtools:BadExpression', ...
                        'Couldn''t evaluate EXPRE: ''%s''.', ...
                        ['if ' rcond ',' rexpr ';end'] ...
                    );
                end
            end

        % field (read/write)
        case {'field'}

            % check condition
            readfield  = ~writemode;
            writefield =  writemode;
            if ~isempty(rcond)
                try
                    readfield  = false;
                    writefield = false;
                    eval(['if ' rcond ',' ...
                          'readfield=~writemode;' ...
                          'writefield=writemode;' ...
                          'end']);
                catch
                    error( ...
                        'BVQXtools:BadExpression', ...
                        'Couldn''t evaluation COND expression: ''%s''.', ...
                        rcond ...
                    );
                end
            end

            % if we're reading the data
            if readfield

                % treat transio correctly
                if ~isinf(tiosz) && ...
                    (rdsks * prod(rdim)) >= tiosz && ...
                    strcmp(rdisk, rdata)
                    try
                        cfpos = ftell(fid);
                        rsize = rdsks * prod(rdim);
                        fseek(fid, rsize, 0);
                        if ftell(fid) ~= (cfpos + rsize)
                            error('SEEK_ERROR');
                        end
                        tio_obj = struct(tioobjt);
                        tio_obj.DataType = rdisk;
                        tio_obj.TypeSize = rdsks;
                        tio_obj.IOOffset = cfpos;
                        obs = rdim;
                        while ~isempty(obs) && ...
                            obs(end) == 1
                            obs(end) = [];
                        end
                        while length(obs) < 2
                            obs(end+1) = 1;
                        end
                        tio_obj.DataDims = obs;
                        tio_obj = transio(0, 'makeobject', tio_obj);
                        try
                            tio_obj(1);
                        catch
                            fid = fopen(filename, 'r', tiole);
                            fseek(fid, cfpos + rdsks, -1);
                        end
                        eval(['bffcont.' rexpr '=tio_obj; clear tio_obj;']);
                    catch
                        error( ...
                            'BVQXtools:TransIOFailed', ...
                            'Error creating transio object.' ...
                        );
                    end
                    rulec = rulec + 1;
                    continue;
                end

                % what disk-bound datatype
                switch (rdisk)

                    % built-in types
                    case { ...
                             'char',  'int8',  'int16',  'int32', ...
                            'uchar', 'uint8', 'uint16', 'uint32', ...
                            'int64', 'uint64','single', 'double'}

                        % do read verbosely ?
                        if verbosemode
                            readdata = verboseread(fid, rdim, rdisk, rexpr);
                        else
                            readdata = ...
                                fread(fid, [1, prod(rdim)], ['*' rdisk]);
                            readdata = reshape(readdata, rdim);
                        end

                    % C-style strings
                    case {'cstring'}

                        % for multiple strings (dim > 1)
                        if prod(rdim) > 1

                            % generate cell structure for strings
                            readdata = cell(rdim);

                            % do read verbosely ?
                            if verbosemode

                                % read strings into structure
                                for strc = 1:prod(rdim)
                                    readdata{strc} = ...
                                        verbosereadcstring(fid, ...
                                        sprintf('%s{%d}', rexpr, strc));
                                end

                            % otherwise
                            else

                                % read strings into structure
                                for strc = 1:prod(rdim)
                                    readdata{strc} = freadcstring(fid);
                                end
                            end

                        % only one string
                        else

                            % do read verbosely ?
                            if verbosemode
                                readdata = verbosereadcstring(fid, rexpr);
                            else
                                readdata = freadcstring(fid);
                            end
                        end

                    % unknown datatype
                    otherwise
                        error( ...
                            'BVQXtools:BadBFFSpec', ...
                            'Invalid DISKTYPE specified: ''%s''.', ...
                            rdisk ...
                        );
                end

                % check if we need type conversion
                if ~strcmp(rdisk, rdata)

                    % what type
                    switch (rdata)

                        % built-in types
                        case { ...
                                 'int8',  'int16',  'int32',  'int64', ...
                                'uint8', 'uint16', 'uint32', 'uint64', ...
                                 'char', 'single', 'double', 'logical'}

                            % try to convert via internal function
                            try
                                eval(['readdata=' rdata '(readdata);']);
                            catch
                                error( ...
                                    'BVQXtools:ConversionFailed', ...
                                    'Built-in conversion failed: %s', ...
                                    lasterr ...
                                );
                            end

                        % all other types
                        otherwise

                            % try to convert data
                            try
                                eval(['readdata=' ...
                                    rdisk '2' rdata '(readdata);']);
                            catch
                                error( ...
                                    'BVQXtools:EvaluationFailed', ...
                                    'Failed data conversion, %s=>%s: %s', ...
                                    rdisk, rdata, lasterr ...
                                );
                            end
                    end
                end

                % put the data read into the structure
                try
                    eval(['bffcont.' rexpr '=readdata; clear readdata;']);
                catch
                    error( ...
                        'BVQXtools:EvaluationFailed', ...
                        'Error storing read data into struct: %s.', ...
                        lasterr ...
                    );
                end

            % are we else writing the data
            elseif writefield

                % get data to write
                try
                    towritedata = [];
                    eval(['towritedata=bffcont.' rexpr ';']);
                catch
                    error( ...
                        'BVQXtools:EvaluationFailed', ...
                        'Error retrieving data from struct: ''%s''.', ...
                        lasterr ...
                    );
                end

                % treat transio correctly
                if istransio(towritedata, true)
                    stio = struct(towritedata);
                    try
                        cfpos = ftell(fid);
                        tfid = 0;
                        if stio.LittleND
                            tfid = fopen(stio.FileName, 'rb', 'ieee-le');
                        else
                            tfid = fopen(stio.FileName, 'rb', 'ieee-le');
                        end
                        twcls = stio.DataType;
                        twicl = ['*' twcls];
                        twsiz = stio.TypeSize;
                        twchk = 2097152 / twsiz;
                        fseek(tfid, stio.IOOffset, -1);
                        twnum = prod(stio.DataDims);
                        while twnum >= twchk
                            fwrite(fid, fread(tfid, [twchk, 1], twicl), twcls);
                            twnum = twnum - twchk;
                        end
                        if twnum > 0
                            fwrite(fid, fread(tfid, [twnum, 1], twicl), twcls);
                        end
                    catch
                        error( ...
                            'BVQXtools:TransIOFailed', ...
                            'Error skipping transio object size in file.' ...
                        );
                    end
                    if tfid > 0
                        fclose(tfid);
                    end
                    stio.FileName = filename;
                    stio.IOOffset = cfpos;
                    eval(['bffcont.' rexpr '=transio(0,''makeobject'',stio);']);
                    rulec = rulec + 1;
                    continue;
                end

                % assume built-in type
                builtintype = true;

                % what current datatype
                switch (rdata)

                    % built-in datatypes
                    case { ...
                             'int8',  'int16',  'int32',  'int64', ...
                            'uint8', 'uint16', 'uint32', 'uint64', ...
                             'char', 'single', 'double', 'logical', ...
                            'cstring'}

                        % do nothing here

                    % otherwise we need conversion!
                    otherwise
                        builtintype = false;
                end

                % if datatype isn't built-in
                if ~builtintype

                    % try to convert data
                    try
                        eval(['towritedata=' ...
                            rdata '2' rdisk '(towritedata);']);
                    catch
                        error( ...
                            'BVQXtools:EvaluationFailed', ...
                            'Failed data conversion, %s=>%s: %s', ...
                            rdata, rdisk, lasterr ...
                        );
                    end

                % datatypes still must be converted
                elseif ~strcmp(rdisk, rdata)

                    % try internal conversion
                    try
                        eval(['towritedata=' rdisk '(towritedata);']);
                    catch
                        error( ...
                            'BVQXtools:EvaluationFailed', ...
                            'Failed data conversion, %s=>%s: %s', ...
                            rdata, rdisk, lasterr ...
                        );
                    end
                end

                % write REAL built-in datatypes
                if ~strcmp(rdisk, 'cstring')

                    % verbose
                    if verbosemode
                        try
                            verbosewrite(fid, towritedata, rdisk, rexpr);
                        catch
                            error( ...
                                'BVQXtools:WriteFailed', ...
                                'Writing to file failed: ''%s''.', ...
                                lasterr ...
                            );
                        end

                    % non-verbose
                    else
                        try
                            fwrite(fid, towritedata, rdisk);
                        catch
                            error( ...
                                'BVQXtools:WriteFailed', ...
                                'Writing to file failed: ''%s''.', ...
                                lasterr ...
                            );
                        end
                    end

                % c-style strings
                else

                    % one string
                    if ischar(towritedata)

                        % verbose
                        if verbosemode
                            try
                                verbosewritecstring(fid, towritedata, rexpr);
                            catch
                                error( ...
                                    'BVQXtools:WriteFailed', ...
                                    'Writing to file failed: ''%s''.', ...
                                    lasterr ...
                                );
                            end

                        % non-verbose
                        else
                            try
                                fwrite(fid, ...
                                    uint8(double(towritedata(:)')), ...
                                    'uint8');
                                fwrite(fid, 0, 'uint8');
                            catch
                                error( ...
                                    'BVQXtools:WriteFailed', ...
                                    'Writing to file failed: ''%s''.', ...
                                    lasterr ...

⌨️ 快捷键说明

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