📄 ascii2num.m
字号:
function[varargout]=ascii2num(varargin)%ASCII2NUM Convert ASCII values for numbers into numeric values.%% N=ASCII2NUM(A) converts a matrix A of ASCII charcter codes, % reprenting numbers, into a vector of numeric values N. %% Thus, CHAR(A) would be a string array oriented as follows%% CHAR(A) = [ ' -6.1512'; % '20103.1321'; % '- 010.1232']. %% CHAR(A) may include numbers, minus signs, and decimal points. % If a decimal point exists, it must be in the same column for % all rows. Minus signs may be present in any row. % % [N1,N2,... NN]=ASCII2NUM(A1,A2,... AN) also works.%% For large A, ASCII2NUM is about 1-2 orders of magnitude faster% than the built-in Matlab alternative, STR2NUM(CHAR(A)).%% ASCII2NUM is useful in reading in large files of ASCII data.%% 'ascii2num --t' runs a test.% % Usage: n=ascii2num(a);% [n1,n2,n3]=ascii2num(a1,a2,a3);% __________________________________________________________________% This is part of JLAB --- type 'help jlab' for more information% (C) 2006 J.M. Lilly --- type 'help jlab_license' for details if strcmp(varargin{1}, '--t') ascii2num_test,returnend % na=nargin;% btest=0;% if strcmp(varargin{end},'tes')% btest=1;% na=na-1;% varargin=varargin(1:end-1);% endfor i=1:nargin x=varargin{1}; varargout{i}=ascii2num1(x);endfunction[y]=ascii2num1(x)%Find and remove all minus signsindex=find(x==real('-'));sgn=ones(size(x(:,1)));if ~isempty(index) x(index)=real(' '); [ii,jj]=ind2sub(size(x),index); sgn(ii)=-1; bool=zeros(size(x)); bool(index)=1; if anyany(sum(bool,2)>1) error('More than one minus sign in at least one row.') endendvswap(x,real(' '), real('0'));%Does there exist a decimal place?jd=find(x(1,:)==real('.'));if isempty(jd) jd=size(x,2)+1;elseif length(jd)>1 error('More than one decimal point in first row.')elseif length(jd)==1 b=all(x(:,jd)==real('.')); if ~b error('Decimal point does not exist in all rows.') end x=x(:,[1:jd-1 jd+1:end]); if any(find(x==real('.'))) error('Decimal points exist in more than one column') endend%JD is now column of tenths placetens=10.^([jd-2:-1:jd-size(x,2)-1]);y=(x-48)*tens';y=y.*sgn;function[]=ascii2num_testtol=1e-10;clear xx(1,:)='2006.152';x(2,:)='2007.132';x=real(x);reporttest('ASCII2NUM no minus signs',aresame(ascii2num(x),str2num(char(x)),tol))clear xx(1,:)=' - 6.152';x(2,:)='-2007.132';x=real(x);reporttest('ASCII2NUM with minus signs',aresame(ascii2num(x),str2num(char(x)),tol))N=10000;x=round(rand(N,1)*1000*1000)/1000;x=vnum2str(x,-3);x=real(x);tic;y1=str2num(char(x));et1=toc;tic;y2=ascii2num(x);et2=toc;reporttest('ASCII2NUM random',aresame(y1,y2,tol)) warning offdisp(['ASCII2NUM(A) was ' num2str(et1./et2,3) ' times faster than STR2NUM(CHAR(A)).'])warning on
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -