5187
|
1 ## Copyright (C) 2004 by Alois Schloegl |
5185
|
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 |
5187
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{num}, @var{status}, @var{strarray}] =} str2double (@var{str}, @var{cdelim}, @var{rdelim}, @var{ddelim}) |
|
22 ## Convert strings into numeric values. |
5184
|
23 ## |
5187
|
24 ## @code{str2double} can replace @code{str2num}, but avoids the use of |
|
25 ## @code{eval} on unknown data. |
5184
|
26 ## |
5187
|
27 ## @var{str} can be the form @samp{[+-]d[.]dd[[eE][+-]ddd]} in which |
|
28 ## @samp{d} can be any of digit from 0 to 9, and @samp{[]} indicate |
|
29 ## optional elements. |
|
30 ## |
|
31 ## @var{num} is the corresponding numeric value. If the conversion |
|
32 ## fails, status is -1 and @var{num} is NaN. |
5184
|
33 ## |
5187
|
34 ## @var{status} is 0 if the conversion was successful and -1 otherwise. |
|
35 ## |
|
36 ## @var{strarray} is a cell array of strings. |
|
37 ## |
|
38 ## Elements which are not defined or not valid return NaN and the |
|
39 ## @var{status} becomes -1. |
5184
|
40 ## |
5187
|
41 ## If @var{str} is a character array or a cell array of strings, then |
|
42 ## @var{num} and @var{status} return matrices of appropriate size. |
|
43 ## |
|
44 ## @var{str} can also contain multiple elements separated by row and |
|
45 ## column delimiters (@var{cdelim} and @var{rdelim}). |
|
46 ## |
|
47 ## The parameters @var{cdelim}, @var{rdelim}, and @var{ddelim} are |
|
48 ## optional column, row, and decimal delimiters. |
5184
|
49 ## |
5187
|
50 ## The default row-delimiters are newline, carriage return and semicolon |
|
51 ## (ASCII 10, 13 and 59). The default column-delimiters are tab, space |
|
52 ## and comma (ASCII 9, 32, and 44). The default decimal delimiter is |
|
53 ## @samp{.} (ASCII 46). |
5184
|
54 ## |
5187
|
55 ## @var{cdelim}, @var{rdelim}, and @var{ddelim} must contain only nul, |
|
56 ## newline, carriage return, semicolon, colon, slash, tab, space, comma, |
|
57 ## or @samp{()[]@{@}} (ASCII 0, 9, 10, 11, 12, 13, 14, 32, 33, 34, 40, |
|
58 ## 41, 44, 47, 58, 59, 91, 93, 123, 124, 125). |
|
59 ## |
|
60 ## Examples: |
|
61 ## |
|
62 ## @example |
|
63 ## str2double ("-.1e-5") |
|
64 ## @result{} -1.0000e-006 |
5184
|
65 ## |
5187
|
66 ## str2double (".314e1, 44.44e-1, .7; -1e+1") |
|
67 ## @result{} |
|
68 ## 3.1400 4.4440 0.7000 |
|
69 ## -10.0000 NaN NaN |
5184
|
70 ## |
5187
|
71 ## line = "200,300,400,NaN,-inf,cd,yes,no,999,maybe,NaN"; |
|
72 ## [x, status] = str2double (line) |
|
73 ## x = |
|
74 ## 200 300 400 NaN -Inf NaN NaN NaN 999 NaN NaN |
|
75 ## status = |
|
76 ## 0 0 0 0 0 -1 -1 -1 0 -1 0 |
|
77 ## @end example |
|
78 ## @end deftypefn |
|
79 |
|
80 ## Author: Alois Schloegl <a.schloegl@ieee.org> |
|
81 ## Adapted-by: jwe |
5183
|
82 |
5185
|
83 function [num, status, strarray] = str2double (s, cdelim, rdelim, ddelim) |
|
84 |
|
85 FLAG_OCTAVE = exist('OCTAVE_VERSION','builtin'); |
|
86 |
|
87 ## digits, sign, exponent,NaN,Inf |
|
88 ## valid_char = '0123456789eE+-.nNaAiIfF'; |
5183
|
89 |
5185
|
90 ## valid delimiters |
|
91 valid_delim = char (sort ([0, 9:14, 32:34, abs("()[]{},;:\"|/")])); |
5184
|
92 |
5185
|
93 if (nargin < 1) |
|
94 error ("missing input argument"); |
|
95 endif |
5184
|
96 |
5185
|
97 if (nargin < 2) |
|
98 ## column delimiter |
|
99 cdelim = char ([9, 32, abs(",")]); |
|
100 else |
|
101 ## make unique cdelim |
|
102 cdelim = char (sort (cdelim(:))); |
|
103 tmp = [1; 1+find(diff(abs(cdelim))>0)]; |
|
104 cdelim = cdelim(tmp)'; |
|
105 endif |
|
106 |
|
107 if (nargin < 3) |
|
108 ## row delimiter |
|
109 rdelim = char ([0, 10, 13, abs(";")]); |
|
110 else |
|
111 ## make unique rdelim |
|
112 rdelim = char (sort (rdelim(:))); |
|
113 tmp = [1; 1+find(diff(abs(rdelim))>0)]; |
|
114 rdelim = rdelim(tmp)'; |
|
115 endif |
|
116 |
|
117 if (nargin < 4) |
|
118 ddelim = '.'; |
|
119 elseif (length (ddelim) != 1) |
|
120 error ("decimal delimiter must be exactly one character"); |
|
121 endif |
5183
|
122 |
5185
|
123 ## check if RDELIM and CDELIM are distinct |
5184
|
124 |
5185
|
125 delim = sort (abs ([cdelim, rdelim, ddelim])); |
|
126 tmp = [1, 1+find(diff(delim)>0)]; |
|
127 delim = delim(tmp); |
|
128 ## [length(delim),length(cdelim),length(rdelim)] |
|
129 if (length (delim) < (length(cdelim) + length(rdelim))+1) |
|
130 ## length (ddelim) must be one. |
|
131 error ("row, column and decimal delimiter are not distinct"); |
|
132 endif |
5184
|
133 |
5185
|
134 ## check if delimiters are valid |
|
135 tmp = sort (abs ([cdelim, rdelim])); |
|
136 flag = zeros (size (tmp)); |
|
137 k1 = 1; |
|
138 k2 = 1; |
|
139 while (k1 <= length (tmp) && k2 <= length (valid_delim)), |
|
140 if (tmp(k1) == valid_delim(k2)) |
|
141 flag(k1) = 1; |
|
142 k1++; |
|
143 elseif (tmp(k1) < valid_delim(k2)) |
|
144 k1++; |
|
145 elseif (tmp(k1) > valid_delim(k2)) |
|
146 k2++; |
|
147 endif |
|
148 endwhile |
|
149 if (! all (flag)) |
|
150 error ("invalid delimiters!"); |
|
151 endif |
5183
|
152 |
5185
|
153 ## various input parameters |
5183
|
154 |
5185
|
155 if (isnumeric (s)) |
|
156 if (all (s < 256) && all (s >= 0)) |
|
157 s = char (s); |
|
158 else |
|
159 error ("str2double: input variable must be a string"); |
|
160 endif |
|
161 endif |
5183
|
162 |
5185
|
163 if (isempty (s)) |
|
164 num = []; |
|
165 status = 0; |
|
166 return; |
|
167 elseif (iscell (s)) |
|
168 strarray = s; |
|
169 elseif (ischar (s) && all (size (s) > 1)) |
|
170 ## char array transformed into a string. |
|
171 for k = 1:size (s, 1) |
|
172 tmp = find (! isspace (s(k,:))); |
|
173 strarray{k,1} = s(k,min(tmp):max(tmp)); |
|
174 endfor |
|
175 elseif (ischar (s)), |
|
176 num = []; |
|
177 status = 0; |
|
178 strarray = {}; |
|
179 ## add stop sign; makes sure last digit is not skipped |
|
180 s(end+1) = rdelim(1); |
|
181 RD = zeros (size (s)); |
|
182 for k = 1:length (rdelim), |
|
183 RD = RD | (s == rdelim(k)); |
|
184 endfor |
|
185 CD = RD; |
|
186 for k = 1:length (cdelim), |
|
187 CD = CD | (s==cdelim(k)); |
|
188 endfor |
5184
|
189 |
5185
|
190 k1 = 1; # current row |
|
191 k2 = 0; # current column |
|
192 k3 = 0; # current element |
5183
|
193 |
5185
|
194 sl = length (s); |
|
195 ix = 1; |
|
196 ## while (ix < sl) & any(abs(s(ix))==[rdelim,cdelim]), |
|
197 while (ix < sl && CD(ix)) |
|
198 ix++ |
|
199 endwhile |
|
200 ta = ix; |
|
201 te = []; |
|
202 while (ix <= sl) |
|
203 if (ix == sl) |
|
204 te = sl; |
|
205 endif |
|
206 ## if any(abs(s(ix))==[cdelim(1),rdelim(1)]), |
|
207 if (CD(ix)) |
|
208 te = ix - 1; |
|
209 endif |
|
210 if (! isempty (te)) |
|
211 k2++; |
|
212 k3++; |
|
213 strarray{k1,k2} = s(ta:te); |
|
214 ## strarray{k1,k2} = [ta,te]; |
|
215 |
|
216 flag = 0; |
|
217 ## while any(abs(s(ix))==[cdelim(1),rdelim(1)]) & (ix < sl), |
|
218 while (CD(ix) && ix < sl) |
|
219 flag = flag | RD(ix); |
|
220 ix++; |
|
221 endwhile |
5183
|
222 |
5185
|
223 if (flag) |
|
224 k2 = 0; |
|
225 k1++; |
|
226 endif |
|
227 ta = ix; |
|
228 te = []; |
|
229 endif |
|
230 ix++; |
|
231 endwhile |
|
232 else |
|
233 error ("str2double: invalid input argument"); |
|
234 endif |
5184
|
235 |
5185
|
236 [nr, nc]= size (strarray); |
|
237 status = zeros (nr, nc); |
|
238 num = repmat (NaN, nr, nc); |
5184
|
239 |
5185
|
240 for k1 = 1:nr |
|
241 for k2 = 1:nc |
|
242 t = strarray{k1,k2}; |
|
243 if (length (t) == 0) |
|
244 ## return error code |
|
245 status(k1,k2) = -1; |
|
246 num(k1,k2) = NaN; |
|
247 else |
|
248 ## get mantisse |
|
249 g = 0; |
|
250 v = 1; |
|
251 if (t(1) == "-") |
|
252 v = -1; |
|
253 l = min (2, length(t)); |
|
254 elseif (t(1) == "+") |
|
255 l = min (2, length (t)); |
|
256 else |
|
257 l = 1; |
|
258 endif |
5184
|
259 |
5185
|
260 if (strcmpi (t(l:end), "inf")) |
|
261 num(k1,k2) = v*Inf; |
|
262 elseif (strcmpi (t(l:end), "NaN")); |
|
263 num(k1,k2) = NaN; |
|
264 else |
|
265 if (ddelim == ".") |
|
266 t(t==ddelim) = "."; |
|
267 endif |
5187
|
268 [v, tmp2, c] = sscanf(char(t), "%f %s", "C"); |
5185
|
269 ## [v,c,em,ni] = sscanf(char(t),"%f %s"); |
|
270 ## c = c * (ni>length(t)); |
|
271 if (c == 1), |
|
272 num(k1,k2) = v; |
|
273 else |
|
274 num(k1,k2) = NaN; |
|
275 status(k1,k2) = -1; |
|
276 endif |
|
277 endif |
|
278 endif |
|
279 endfor |
|
280 endfor |
5184
|
281 |
5185
|
282 endfunction |