📄 any2ascii.m
字号:
function asciirep = any2ascii(anyvar, varargin)
% any2ascii - packs simple as well as complex variables into ascii
%
% FORMAT: asciirep = any2ascii(anyvar [,precision, formatted])
%
% Input fields:
%
% anyvar any non-object variable/array
% precision internal precision for num2str call [default := 8]
% formatted if given and not empty output is lazily formatted
%
% Output fields:
%
% asciirep result string
%
% NOTE 1) to re-obtain the original contents of a variable, just use eval:
% prompt/script> copied = eval(asciirep);
% (or)
% prompt/script> eval(['copied = ' asciirep ';']);
%
% NOTE 2) any2ascii should handle the following datatypes:
% - double precision values, including NaN, Inf and complex doubles
% - chars, ranging from simple to formatted strings or 2D arrays
% - cell arrays, as long as they contain other valid input types
% - struct arrays, cell array restriction applies as well
%
% NOTE 3) since this algorithm uses some internals of MATLAB's num2str,
% decimal numbers might lack some precision after transformation!
% for a work-around, you can use 'exact' for precision, which will
% convert doubles to a hexadecimal character representation via
% hxdouble. Only works on first level (with anyvar of type double)
% and requires the hxdouble function to be present when using eval
%
% See also disp, eval, hxdouble
% Version: v0.7b
% Build: 7083014
% Date: Aug-30 2007, 2:36 PM CEST
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% persistent config
persistent a2acfg;
if isempty(a2acfg)
a2acfg.classes = struct( ...
'cell', 'l', ...
'char', 'c', ...
'double', 'd', ...
'single', 'i', ...
'int8', '7', ...
'int16', '5', ...
'int32', '1', ...
'int64', '3', ...
'logical', '9', ...
'struct', 's', ...
'uint8', '8', ...
'uint16', '6', ...
'uint32', '2', ...
'uint64', '4' ...
);
a2acfg.itypes = { ...
'int32', 'uint32', ...
'int64', 'uint64', ...
'int16', 'uint16', ...
'int8', 'uint8', ...
'logical' ...
};
a2acfg.prcs = 8;
end
% enough arguments ?
if nargin < 1
error( ...
'BVQXtools:TooFewArguments',...
'Too few arguments. Try ''help %s''.',...
mfilename ...
);
end
% setup vars
prcs = a2acfg.prcs;
dxct = 0;
% precision in second argument
if nargin > 1 && ...
isnumeric(varargin{1}) && ...
~isempty(varargin{1})
prcs = floor(varargin{1}(1));
% use hxdouble instead
elseif nargin > 1 && ...
ischar(varargin{1})
dxctt = lower(varargin{1}(:)');
if numel(dxctt) == 5 && ...
all(dxctt == 'exact')
dxct = 1;
end
end
% deny negative precision
if prcs < 0
prcs = -prcs;
end
% if precision is 0, no decimal point
if prcs == 0
prcstr = '%0.0f';
% otherwise use %g formatter
else
prcstr = ['%.' int2str(prcs) 'g'];
end
% find out input type and dims
try
% try lookup from persistent car
type = a2acfg.classes.(lower(class(anyvar)));
% error out if type unsupported
catch
error( ...
'BVQXtools:BadArgument',...
'Invalid input type: %s',...
class(anyvar) ...
);
end
% handle sparse matrices differently
if type == 'd' && ...
issparse(anyvar)
type = 'p';
end
% get dimensions
arraydims = size(anyvar);
% no array content
if any(arraydims == 0)
% but not all dims zeros
if ~all(arraydims == 0)
% for numeric types
if any('123456789cdip' == type)
% simply build "empty" zeros with same size
asciirep = ['[zeros(' any2ascii(arraydims) ')]'];
% and prepend correct class cast
if double(type) < 65
asciirep = ['[' a2acfg.itypes{double(type) - 48} ...
'(' asciirep(2:(end-1)) ')]'];
elseif type == 'c'
asciirep = ['[char(' asciirep(2:(end-1)) ')]'];
elseif type == 'i'
asciirep = ['[single(' asciirep(2:(end-1)) ')]'];
elseif type == 'p'
asciirep = ['[sparse(' asciirep(2:(end-1)) ')]'];
end
% empty cell array
elseif type == 'l'
asciirep = ['[cell(' any2ascii(arraydims) ')]'];
% empty struct
else
% get fieldnames
fnams = fieldnames(anyvar);
% empty field list
if numel(fnams) == 0
asciirep = ['[reshape(struct([]),' any2ascii(arraydims) ')]'];
% or has fields
else
% create size argument for cell2struct
dmarg = any2ascii(arraydims);
dmarg = [dmarg(1:(end-1)) ',' num2str(length(fnams)) ']'];
% create cell2struct call
asciirep = ['[cell2struct(cell(' dmarg '),' ...
any2ascii(fnams') ',' ...
num2str(length(arraydims)+1) ')]'];
end
end
% all empty dims
else
% numeric type
if any('123456789cdip' == type)
% generate call
if type == 'd'
asciirep = '[]';
elseif type == 'c'
asciirep = '['''']';
elseif type == 'i'
asciirep = '[single([])]';
elseif type == 'p'
asciirep = '[sparse([])]';
else
asciirep = ['[' a2acfg.itypes{double(type) - 48} '([])]'];
end
% empty cell
elseif type == 'l'
asciirep = '{}';
% empty struct
else
% get fieldnames
fnams = fieldnames(anyvar);
% empty field list
if numel(fnams) == 0
asciirep = '[struct([])]';
% or has fields
else
% create size argument for cell2struct
dmarg = any2ascii(arraydims);
dmarg = [dmarg(1:(end-1)) ',' num2str(length(fnams)) ']'];
% create cell2struct call
asciirep = ['[cell2struct(cell(' dmarg '),' ...
any2ascii(fnams') ',' ...
num2str(length(arraydims)+1) ')]'];
end
end
end
return;
end
% we DO have content
% not a sparse matrix (max 2-D)
if type ~= 'p' && ...
length(arraydims) < 3
% if this is not a cell array, use '[' brackets
if type ~= 'l'
asciirep = '[';
% switch over type
switch type
% standard doubles/singles
case {'d', 'i'}
% doubles without hxdouble or complex content
if type == 'd' && ...
(~dxct || ...
~isreal(anyvar))
% iterate over dims(1)
for outer = 1:arraydims(1)
% set preliminary (line) to empty
line = '';
% iterate over dims(2)
for inner = 1:arraydims(2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -