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

📄 any2ascii.m

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

                        % get single value
                        ivar  = anyvar(outer, inner);

                        % with or without decimals
                        if fix(real(ivar)) == real(ivar)
                            ovar = sprintf('%.0f', ivar);
                        else
                            ovar = sprintf(prcstr, ivar);
                        end

                        % if complex content also give that
                        if ~isreal(ivar)

                            % content >= 0
                            if imag(ivar) >= 0
                                ovar(end + 1) = '+';
                            end

                            % postfix content
                            ovar = [ovar sprintf(prcstr, imag(ivar)) 'i'];
                        end

                        % separate correctly
                        line = [line ovar ','];
                    end

                    % replace last comma by semicolon
                    line(end) = ';';

                    % put line into asciirep
                    asciirep = [asciirep line];
                end

            % yet doubles (use hxdouble then)
            elseif type == 'd'

                % more "lines"
                if arraydims(1) ~= 1

                    % make a reshaped array
                    asciirep = ['[reshape(hxdouble(''' ...
                                  hxdouble(reshape(anyvar, 1, prod(arraydims))) ...
                                  '''),' ...
                                  sprintf('[%d,%d]', arraydims(1), arraydims(2)) ...
                                  ')]'];

                % for one-liners, it's OK to use hxdouble directly
                else
                    asciirep = ['[hxdouble(''' hxdouble(anyvar) ''')]'];
                end

            % only singles remain
            else

                % more "lines"
                if arraydims(1) ~= 1

                    % make a reshaped array
                    asciirep = ['[reshape(hxsingle(''' ...
                                  hxsingle(reshape(anyvar, 1, prod(arraydims))) ...
                                  '''),' ...
                                  sprintf('[%d,%d]', arraydims(1), arraydims(2)) ...
                                  ')]'];

                % for one-liners, it's OK to use hxsingle directly
                else
                    asciirep = ['[hxsingle(''' hxsingle(anyvar) ''')]'];
                end
            end

        % struct array, recursive call for each member value
        case {'s'}

            % get fieldnames and number of names
            fnames = fieldnames(anyvar);
            nnames = length(fnames);

            % we have content
            if nnames

                % just a single (1x1) struct 
                if all(arraydims == 1)

                    % create struct call
                    asciirep = '[struct(';

                    % iterate over names
                    for fcount = 1:nnames

                        % get field (works only on 1x1)
                        cv = anyvar(1).(fnames{fcount});

                        % if not is cell, directly
                        if ~iscell(cv)
                            asciirep = [asciirep '''' fnames{fcount} ''',' ...
                                        any2ascii(cv, prcs, []) ',' ];

                        % otherwise within {} to get call correct
                        else
                            asciirep = [asciirep '''' fnames{fcount} ''',{' ...
                                        any2ascii(cv, prcs, []) '},' ];
                        end
                    end

                    % put to representation
                    asciirep = [asciirep(1:end-1) ');'];

                % multi-struct (array)
                else

                    % use cell2struct/struct2cell pair to convert
                    asciirep = ['[cell2struct(' ...
                                  any2ascii(struct2cell(anyvar)) ',' ...
                                  any2ascii(fnames) ',1);'];
                end

            % no content
            else
                asciirep = sprintf('[cell2struct(cell([0,%d,%d]),{},1)]', ...
                                     arraydims(1),arraydims(2));
            end

        % character arrays
        case {'c'}

            % for empty character arrays
            if prod(arraydims) == 0

                % always give an empty array (MATLAB FIX!)
                asciirep = [asciirep ''''','];

            % with content
            else

                % iterate over "lines"
                for outer = 1:arraydims(1)

                    % replace single quotes
                    ovar = strrep(anyvar(outer, :), '''', '''''');

                    % find illegal characters
                    illegalc = find(ovar<32 | ovar>127);

                    % replace those
                    while ~isempty(illegalc)
                        ovar = strrep(ovar, ovar(illegalc(1)), ...
                               [''' char(' ...
                                sprintf('%d',double(ovar(illegalc(1)))) ...
                                ') ''']);

                        % find anew
                        illegalc = find(ovar<32 | ovar>127);

                        % but keep track of bracket usage!
                        prcs = -1;
                    end

                    % use brackets?
                    if prcs >= 0
                        asciirep = [asciirep '''' ovar '''' ';'];
                    else
                        asciirep = [asciirep '[''' ovar '''' '];'];
                    end
                end
            end

        % int/uint arrays
        otherwise
            asciirep = [asciirep class(anyvar) '(' ...
                        any2ascii(double(anyvar), varargin{2:end}) '),'];

        end

        % either add or replace closing brackets (upon content given)
        if asciirep(end) ~= '['
            asciirep(end) = ']';
        else
            asciirep(end + 1) = ']';
        end

        % third argument
        if nargin > 2 && ...
            isempty(varargin{2}) && ( ...
           ((type == 'd' || ...
             type == 's') && ...
             all(arraydims == 1)) || ...
            (type == 'c' && ...
             arraydims(1) == 1))

            % remove those brackets (for internal use)
            asciirep = asciirep(2:end-1);
        end

        % double brackets?
        while length(asciirep) > 2 && ...
            all(asciirep(1:2) == '[') && ...
            all(asciirep(end-1:end) == ']')

            % remove those anyway
            asciirep=asciirep(2:end-1);
        end

    % cell arrays
    else

        % correct opener
        asciirep = '{';

        % iterate over outer dimension
        for outer = 1:arraydims(1)

            % create "line"'s
            line = '';

            % iterate over inner dimension
            for inner = 1:arraydims(2)
                line = [line any2ascii(anyvar{outer,inner}, prcs, []) ','];
            end
            asciirep = [asciirep line(1:end-1) ';'];
        end

        % on non-empty content
        if asciirep(end) ~= '{'

            % replace final delimiter
            asciirep(end) = '}';

        % for empty content
        else

            % add closing bracket
            asciirep(end + 1) = '}';
        end
    end

% N>2-D, but not a sparse -> add reshape code
elseif type ~= 'p'

    % so pack the contents just as we suspect it to be packed :)
    origdims = sprintf('%d,', arraydims);
    asciirep = ['[reshape(' ...
                any2ascii(reshape(anyvar, 1, prod(arraydims)), varargin{2:end}) ...
                ',' origdims(1:(end-1)) ')]'];

% for sparse matrices, make special code
else

    % get content nicely done
    [spi, spj, sps] = find(anyvar);
    asciirep = ['[sparse(' any2ascii(spi, 0) ',' any2ascii(spj, 0) ',' ...
                any2ascii(sps,varargin{2:end}) ',' ...
                sprintf('%.0f,%.0f', arraydims(1), arraydims(2)) ')]'];

end

% lazy reformatted
if nargin > 2 && ...
  ~isempty(varargin{2}) && ...
   varargin{2} > 0
    lb = [' ...' char(10) char(9)];
    asciirep = strrep(strrep(strrep(asciirep, ...
                      ',',    ', '), ...
                      ';',   [';' lb]), ...
                      '), ', ['),' lb]);
end

⌨️ 快捷键说明

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