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