Mercurial > hg > octave-lyh
comparison scripts/strings/str2double.m @ 5187:e58bbd2b9c94
[project @ 2005-03-03 07:20:32 by jwe]
author | jwe |
---|---|
date | Thu, 03 Mar 2005 07:20:32 +0000 |
parents | 75a442ecd410 |
children | 4c8a2e4e0717 |
comparison
equal
deleted
inserted
replaced
5186:64fcf6e776be | 5187:e58bbd2b9c94 |
---|---|
1 ## Copyright (C) 2004 by Alois Schloegl <a.schloegl@ieee.org> | 1 ## Copyright (C) 2004 by Alois Schloegl |
2 ## | 2 ## |
3 ## This file is part of Octave. | 3 ## This file is part of Octave. |
4 ## | 4 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 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 | 6 ## under the terms of the GNU General Public License as published by |
15 ## You should have received a copy of the GNU General Public License | 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 | 16 ## along with Octave; see the file COPYING. If not, write to the Free |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
18 ## 02111-1307, USA. | 18 ## 02111-1307, USA. |
19 | 19 |
20 ## STR2DOUBLE converts strings into numeric values | 20 ## -*- texinfo -*- |
21 ## [NUM, STATUS,STRARRAY] = STR2DOUBLE(STR) | 21 ## @deftypefn {Function File} {[@var{num}, @var{status}, @var{strarray}] =} str2double (@var{str}, @var{cdelim}, @var{rdelim}, @var{ddelim}) |
22 ## | 22 ## Convert strings into numeric values. |
23 ## STR2DOUBLE can replace STR2NUM, but avoids the insecure use of EVAL | 23 ## |
24 ## on unknown data [1]. | 24 ## @code{str2double} can replace @code{str2num}, but avoids the use of |
25 ## | 25 ## @code{eval} on unknown data. |
26 ## STR can be the form '[+-]d[.]dd[[eE][+-]ddd]' | 26 ## |
27 ## d can be any of digit from 0 to 9, [] indicate optional elements | 27 ## @var{str} can be the form @samp{[+-]d[.]dd[[eE][+-]ddd]} in which |
28 ## NUM is the corresponding numeric value. | 28 ## @samp{d} can be any of digit from 0 to 9, and @samp{[]} indicate |
29 ## if the conversion fails, status is -1 and NUM is NaN. | 29 ## optional elements. |
30 ## STATUS = 0: conversion was successful | 30 ## |
31 ## STATUS = -1: couldnot convert string into numeric value | 31 ## @var{num} is the corresponding numeric value. If the conversion |
32 ## STRARRAY is a cell array of strings. | 32 ## fails, status is -1 and @var{num} is NaN. |
33 ## | 33 ## |
34 ## Elements which are not defined or not valid return NaN and | 34 ## @var{status} is 0 if the conversion was successful and -1 otherwise. |
35 ## the STATUS becomes -1 | 35 ## |
36 ## STR can be also a character array or a cell array of strings. | 36 ## @var{strarray} is a cell array of strings. |
37 ## Then, NUM and STATUS return matrices of appropriate size. | 37 ## |
38 ## | 38 ## Elements which are not defined or not valid return NaN and the |
39 ## STR can also contain multiple elements. | 39 ## @var{status} becomes -1. |
40 ## default row-delimiters are: | 40 ## |
41 ## NEWLINE, CARRIAGE RETURN and SEMICOLON i.e. ASCII 10, 13 and 59. | 41 ## If @var{str} is a character array or a cell array of strings, then |
42 ## default column-delimiters are: | 42 ## @var{num} and @var{status} return matrices of appropriate size. |
43 ## TAB, SPACE and COMMA i.e. ASCII 9, 32, and 44. | 43 ## |
44 ## default decimal delimiter is '.' char(46), sometimes (e.g in | 44 ## @var{str} can also contain multiple elements separated by row and |
45 ## Tab-delimited text files generated by Excel export in Europe) | 45 ## column delimiters (@var{cdelim} and @var{rdelim}). |
46 ## might used ',' as decimal delimiter. | 46 ## |
47 ## | 47 ## The parameters @var{cdelim}, @var{rdelim}, and @var{ddelim} are |
48 ## [NUM, STATUS] = STR2DOUBLE(STR,CDELIM,RDELIM,DDELIM) | 48 ## optional column, row, and decimal delimiters. |
49 ## CDELIM .. [OPTIONAL] user-specified column delimiter | 49 ## |
50 ## RDELIM .. [OPTIONAL] user-specified row delimiter | 50 ## The default row-delimiters are newline, carriage return and semicolon |
51 ## DDELIM .. [OPTIONAL] user-specified decimal delimiter | 51 ## (ASCII 10, 13 and 59). The default column-delimiters are tab, space |
52 ## CDELIM, RDELIM and DDELIM must contain only | 52 ## and comma (ASCII 9, 32, and 44). The default decimal delimiter is |
53 ## NULL, NEWLINE, CARRIAGE RETURN, SEMICOLON, COLON, SLASH, TAB, SPACE, COMMA, or ()[]{} | 53 ## @samp{.} (ASCII 46). |
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 | 54 ## |
55 ## | 55 ## @var{cdelim}, @var{rdelim}, and @var{ddelim} must contain only nul, |
56 ## Examples: | 56 ## newline, carriage return, semicolon, colon, slash, tab, space, comma, |
57 ## str2double('-.1e-5') | 57 ## or @samp{()[]@{@}} (ASCII 0, 9, 10, 11, 12, 13, 14, 32, 33, 34, 40, |
58 ## ans = -1.0000e-006 | 58 ## 41, 44, 47, 58, 59, 91, 93, 123, 124, 125). |
59 ## | 59 ## |
60 ## str2double('.314e1, 44.44e-1, .7; -1e+1') | 60 ## Examples: |
61 ## ans = | 61 ## |
62 ## 3.1400 4.4440 0.7000 | 62 ## @example |
63 ## -10.0000 NaN NaN | 63 ## str2double ("-.1e-5") |
64 ## | 64 ## @result{} -1.0000e-006 |
65 ## line ='200,300,400,NaN,-inf,cd,yes,no,999,maybe,NaN'; | 65 ## |
66 ## [x,status]=str2double(line) | 66 ## str2double (".314e1, 44.44e-1, .7; -1e+1") |
67 ## x = | 67 ## @result{} |
68 ## 200 300 400 NaN -Inf NaN NaN NaN 999 NaN NaN | 68 ## 3.1400 4.4440 0.7000 |
69 ## status = | 69 ## -10.0000 NaN NaN |
70 ## 0 0 0 0 0 -1 -1 -1 0 -1 0 | 70 ## |
71 ## | 71 ## line = "200,300,400,NaN,-inf,cd,yes,no,999,maybe,NaN"; |
72 ## Reference(s): | 72 ## [x, status] = str2double (line) |
73 ## [1] David A. Wheeler, Secure Programming for Linux and Unix HOWTO. | 73 ## x = |
74 ## http://en.tldp.org/HOWTO/Secure-Programs-HOWTO/ | 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 | |
75 | 82 |
76 function [num, status, strarray] = str2double (s, cdelim, rdelim, ddelim) | 83 function [num, status, strarray] = str2double (s, cdelim, rdelim, ddelim) |
77 | 84 |
78 FLAG_OCTAVE = exist('OCTAVE_VERSION','builtin'); | 85 FLAG_OCTAVE = exist('OCTAVE_VERSION','builtin'); |
79 | 86 |
256 num(k1,k2) = NaN; | 263 num(k1,k2) = NaN; |
257 else | 264 else |
258 if (ddelim == ".") | 265 if (ddelim == ".") |
259 t(t==ddelim) = "."; | 266 t(t==ddelim) = "."; |
260 endif | 267 endif |
261 [v,tmp2,c] = sscanf(char(t), "%f %s", "C"); | 268 [v, tmp2, c] = sscanf(char(t), "%f %s", "C"); |
262 ## [v,c,em,ni] = sscanf(char(t),"%f %s"); | 269 ## [v,c,em,ni] = sscanf(char(t),"%f %s"); |
263 ## c = c * (ni>length(t)); | 270 ## c = c * (ni>length(t)); |
264 if (c == 1), | 271 if (c == 1), |
265 num(k1,k2) = v; | 272 num(k1,k2) = v; |
266 else | 273 else |