5183
|
1 function [num,status,strarray] = str2double(s,cdelim,rdelim,ddelim) |
|
2 %% STR2DOUBLE converts strings into numeric values |
|
3 %% [NUM, STATUS,STRARRAY] = STR2DOUBLE(STR) |
|
4 %% |
|
5 %% STR2DOUBLE can replace STR2NUM, but avoids the insecure use of EVAL |
|
6 %% on unknown data [1]. |
|
7 %% |
|
8 %% STR can be the form '[+-]d[.]dd[[eE][+-]ddd]' |
|
9 %% d can be any of digit from 0 to 9, [] indicate optional elements |
|
10 %% NUM is the corresponding numeric value. |
|
11 %% if the conversion fails, status is -1 and NUM is NaN. |
|
12 %% STATUS = 0: conversion was successful |
|
13 %% STATUS = -1: couldnot convert string into numeric value |
|
14 %% STRARRAY is a cell array of strings. |
|
15 %% |
|
16 %% Elements which are not defined or not valid return NaN and |
|
17 %% the STATUS becomes -1 |
|
18 %% STR can be also a character array or a cell array of strings. |
|
19 %% Then, NUM and STATUS return matrices of appropriate size. |
|
20 %% |
|
21 %% STR can also contain multiple elements. |
|
22 %% default row-delimiters are: |
|
23 %% NEWLINE, CARRIAGE RETURN and SEMICOLON i.e. ASCII 10, 13 and 59. |
|
24 %% default column-delimiters are: |
|
25 %% TAB, SPACE and COMMA i.e. ASCII 9, 32, and 44. |
|
26 %% default decimal delimiter is '.' char(46), sometimes (e.g in |
|
27 %% Tab-delimited text files generated by Excel export in Europe) |
|
28 %% might used ',' as decimal delimiter. |
|
29 %% |
|
30 %% [NUM, STATUS] = STR2DOUBLE(STR,CDELIM,RDELIM,DDELIM) |
|
31 %% CDELIM .. [OPTIONAL] user-specified column delimiter |
|
32 %% RDELIM .. [OPTIONAL] user-specified row delimiter |
|
33 %% DDELIM .. [OPTIONAL] user-specified decimal delimiter |
|
34 %% CDELIM, RDELIM and DDELIM must contain only |
|
35 %% NULL, NEWLINE, CARRIAGE RETURN, SEMICOLON, COLON, SLASH, TAB, SPACE, COMMA, or ()[]{} |
|
36 %% i.e. ASCII 0,9,10,11,12,13,14,32,33,34,40,41,44,47,58,59,91,93,123,124,125 |
|
37 %% |
|
38 %% Examples: |
|
39 %% str2double('-.1e-5') |
|
40 %% ans = -1.0000e-006 |
|
41 %% |
|
42 %% str2double('.314e1, 44.44e-1, .7; -1e+1') |
|
43 %% ans = |
|
44 %% 3.1400 4.4440 0.7000 |
|
45 %% -10.0000 NaN NaN |
|
46 %% |
|
47 %% line ='200,300,400,NaN,-inf,cd,yes,no,999,maybe,NaN'; |
|
48 %% [x,status]=str2double(line) |
|
49 %% x = |
|
50 %% 200 300 400 NaN -Inf NaN NaN NaN 999 NaN NaN |
|
51 %% status = |
|
52 %% 0 0 0 0 0 -1 -1 -1 0 -1 0 |
|
53 %% |
|
54 %% Reference(s): |
|
55 %% [1] David A. Wheeler, Secure Programming for Linux and Unix HOWTO. |
|
56 %% http://en.tldp.org/HOWTO/Secure-Programs-HOWTO/ |
|
57 |
|
58 %% This program is free software; you can redistribute it and/or |
|
59 %% modify it under the terms of the GNU General Public License |
|
60 %% as published by the Free Software Foundation; either version 2 |
|
61 %% of the License, or (at your option) any later version. |
|
62 %% |
|
63 %% This program is distributed in the hope that it will be useful, |
|
64 %% but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
65 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
66 %% GNU General Public License for more details. |
|
67 %% |
|
68 %% You should have received a copy of the GNU General Public License |
|
69 %% along with this program; if not, write to the Free Software |
|
70 %% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
71 |
|
72 %% $Revision: 1.11 $ |
|
73 %% $Id: str2double.m,v 1.11 2004/06/23 20:52:32 schloegl Exp $ |
|
74 %% Copyright (C) 2004 by Alois Schloegl <a.schloegl@ieee.org> |
|
75 %% This function is part of Octave-Forge http://octave.sourceforge.net/ |
|
76 |
|
77 FLAG_OCTAVE = exist('OCTAVE_VERSION','builtin'); |
|
78 |
|
79 % valid_char = '0123456789eE+-.nNaAiIfF'; % digits, sign, exponent,NaN,Inf |
|
80 valid_delim = char(sort([0,9:14,32:34,abs('()[]{},;:"|/')])); % valid delimiter |
|
81 if nargin < 1, |
|
82 error('missing input argument.') |
|
83 end; |
|
84 if nargin < 2, |
|
85 cdelim = char([9,32,abs(',')]); % column delimiter |
|
86 else |
|
87 % make unique cdelim |
|
88 cdelim = char(sort(cdelim(:))); |
|
89 tmp = [1;1+find(diff(abs(cdelim))>0)]; |
|
90 cdelim = cdelim(tmp)'; |
|
91 end; |
|
92 if nargin < 3, |
|
93 rdelim = char([0,10,13,abs(';')]); % row delimiter |
|
94 else |
|
95 % make unique rdelim |
|
96 rdelim = char(sort(rdelim(:))); |
|
97 tmp = [1;1+find(diff(abs(rdelim))>0)]; |
|
98 rdelim = rdelim(tmp)'; |
|
99 end; |
|
100 if nargin<4, |
|
101 ddelim = '.'; |
|
102 elseif length(ddelim)~=1, |
|
103 error('decimal delimiter must be exactly one character.'); |
|
104 end; |
|
105 |
|
106 % check if RDELIM and CDELIM are distinct |
|
107 delim = sort(abs([cdelim,rdelim,ddelim])); |
|
108 tmp = [1, 1 + find(diff(delim)>0)]; |
|
109 delim = delim(tmp); |
|
110 %[length(delim),length(cdelim),length(rdelim)] |
|
111 if length(delim) < (length(cdelim)+length(rdelim))+1, % length(ddelim) must be one. |
|
112 error('row, column and decimal delimiter are not distinct.'); |
|
113 end; |
|
114 |
|
115 % check if delimiters are valid |
|
116 tmp = sort(abs([cdelim,rdelim])); |
|
117 flag = zeros(size(tmp)); |
|
118 k1 = 1; |
|
119 k2 = 1; |
|
120 while (k1 <= length(tmp)) & (k2 <= length(valid_delim)), |
|
121 if tmp(k1) == valid_delim(k2), |
|
122 flag(k1) = 1; |
|
123 k1 = k1 + 1; |
|
124 elseif tmp(k1) < valid_delim(k2), |
|
125 k1 = k1 + 1; |
|
126 elseif tmp(k1) > valid_delim(k2), |
|
127 k2 = k2 + 1; |
|
128 end; |
|
129 end; |
|
130 if ~all(flag), |
|
131 error('Invalid delimiters!'); |
|
132 end; |
|
133 |
|
134 %%%%% various input parameters |
|
135 if isnumeric(s) |
|
136 if all(s<256) & all(s>=0) |
|
137 s = char(s); |
|
138 else |
|
139 error('STR2DOUBLE: input variable must be a string') |
|
140 end; |
|
141 end; |
|
142 |
|
143 if isempty(s), |
|
144 num = []; |
|
145 status = 0; |
|
146 return; |
|
147 |
|
148 elseif iscell(s), |
|
149 strarray = s; |
|
150 |
|
151 elseif ischar(s) & all(size(s)>1), %% char array transformed into a string. |
|
152 for k = 1:size(s,1), |
|
153 tmp = find(~isspace(s(k,:))); |
|
154 strarray{k,1} = s(k,min(tmp):max(tmp)); |
|
155 end; |
|
156 |
|
157 elseif ischar(s), |
|
158 num = []; |
|
159 status = 0; |
|
160 strarray = {}; |
|
161 |
|
162 s(end+1) = rdelim(1); % add stop sign; makes sure last digit is not skipped |
|
163 |
|
164 RD = zeros(size(s)); |
|
165 for k = 1:length(rdelim), |
|
166 RD = RD | (s==rdelim(k)); |
|
167 end; |
|
168 CD = RD; |
|
169 for k = 1:length(cdelim), |
|
170 CD = CD | (s==cdelim(k)); |
|
171 end; |
|
172 |
|
173 k1 = 1; % current row |
|
174 k2 = 0; % current column |
|
175 k3 = 0; % current element |
|
176 |
|
177 sl = length(s); |
|
178 ix = 1; |
|
179 %while (ix < sl) & any(abs(s(ix))==[rdelim,cdelim]), |
|
180 while (ix < sl) & CD(ix), |
|
181 ix = ix + 1; |
|
182 end; |
|
183 ta = ix; te = []; |
|
184 while ix <= sl; |
|
185 if (ix == sl), |
|
186 te = sl; |
|
187 end; |
|
188 %if any(abs(s(ix))==[cdelim(1),rdelim(1)]), |
|
189 if CD(ix), |
|
190 te = ix - 1; |
|
191 end; |
|
192 if ~isempty(te), |
|
193 k2 = k2 + 1; |
|
194 k3 = k3 + 1; |
|
195 strarray{k1,k2} = s(ta:te); |
|
196 %strarray{k1,k2} = [ta,te]; |
|
197 |
|
198 flag = 0; |
|
199 %while any(abs(s(ix))==[cdelim(1),rdelim(1)]) & (ix < sl), |
|
200 while CD(ix) & (ix < sl), |
|
201 flag = flag | RD(ix); |
|
202 ix = ix + 1; |
|
203 end; |
|
204 |
|
205 if flag, |
|
206 k2 = 0; |
|
207 k1 = k1 + 1; |
|
208 end; |
|
209 ta = ix; |
|
210 te = []; |
|
211 end; |
|
212 ix = ix + 1; |
|
213 end; |
|
214 else |
|
215 error('STR2DOUBLE: invalid input argument'); |
|
216 end; |
|
217 |
|
218 [nr,nc]= size(strarray); |
|
219 status = zeros(nr,nc); |
|
220 num = repmat(NaN,nr,nc); |
|
221 |
|
222 for k1 = 1:nr, |
|
223 for k2 = 1:nc, |
|
224 t = strarray{k1,k2}; |
|
225 if (length(t)==0), |
|
226 status(k1,k2) = -1; %% return error code |
|
227 num(k1,k2) = NaN; |
|
228 else |
|
229 %% get mantisse |
|
230 g = 0; |
|
231 v = 1; |
|
232 if t(1)=='-', |
|
233 v = -1; |
|
234 l = min(2,length(t)); |
|
235 elseif t(1)=='+', |
|
236 l = min(2,length(t)); |
|
237 else |
|
238 l = 1; |
|
239 end; |
|
240 |
|
241 if strcmpi(t(l:end),'inf') |
|
242 num(k1,k2) = v*inf; |
|
243 |
|
244 elseif strcmpi(t(l:end),'NaN'); |
|
245 num(k1,k2) = NaN; |
|
246 |
|
247 else |
|
248 if ddelim=='.', |
|
249 t(t==ddelim)='.'; |
|
250 end; |
|
251 if FLAG_OCTAVE, |
|
252 [v,tmp2,c] = sscanf(char(t),'%f %s','C'); |
|
253 else |
|
254 [v,c,em,ni] = sscanf(char(t),'%f %s'); |
|
255 c = c * (ni>length(t)); |
|
256 end; |
|
257 if (c==1), |
|
258 num(k1,k2) = v; |
|
259 else |
|
260 num(k1,k2) = NaN; |
|
261 status(k1,k2) = -1; |
|
262 end |
|
263 end |
|
264 end; |
|
265 end; |
|
266 end; |
|
267 |