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