time | Calls | line |
---|
| | 1 | function x = str2double(s)
|
| | 2 | %STR2DOUBLE Convert string to double precision value
|
| | 3 | % X = STR2DOUBLE(S) converts S, text that represents a real or complex
|
| | 4 | % scalar value, to a double precision number. S may contain digits,
|
| | 5 | % a comma (thousands separator), a decimal point, a leading + or - sign,
|
| | 6 | % an 'e' preceding a power of 10 scale factor, and an 'i' for
|
| | 7 | % a complex unit. S can be a character vector or a string scalar.
|
| | 8 | %
|
| | 9 | % If S does not represent a valid scalar value, STR2DOUBLE(S) returns NaN.
|
| | 10 | %
|
| | 11 | % X = STR2DOUBLE(STR) converts the elements in string array STR to double.
|
| | 12 | % The matrix X returned will be the same size as STR. NaN is returned for
|
| | 13 | % any string element that does not represent a valid scalar value.
|
| | 14 | %
|
| | 15 | % X = STR2DOUBLE(C) converts the elements in the cell array of character
|
| | 16 | % vectors C to double. The matrix X returned will be the same size as C.
|
| | 17 | % NaN is returned for any cell that does not represent a valid
|
| | 18 | % scalar value. NaN is returned for individual cells in C that are
|
| | 19 | % cell arrays.
|
| | 20 | %
|
| | 21 | % Examples
|
| | 22 | % str2double('123.45e7')
|
| | 23 | % str2double('123 + 45i')
|
| | 24 | % str2double('3.14159')
|
| | 25 | % str2double('2.7i - 3.14')
|
| | 26 | % str2double({'2.71' '3.1415'})
|
| | 27 | % str2double('1,200.34')
|
| | 28 | %
|
| | 29 | % See also STR2NUM, NUM2STR, HEX2NUM, CHAR, STRING.
|
| | 30 |
|
| | 31 | % Copyright 1984-2017 The MathWorks, Inc.
|
| | 32 |
|
< 0.001 | 1 | 33 | if ischar(s)
|
| | 34 |
|
| | 35 | % Try to read simple case of something like 5.75
|
< 0.001 | 1 | 36 | [a,count,errmsg,nextindex] = sscanf(s,'%f',1);
|
< 0.001 | 1 | 37 | if count == 1 && isempty(errmsg) && nextindex > numel(s)
|
| 1 | 38 | x = a;
|
< 0.001 | 1 | 39 | return;
|
| | 40 | end
|
| | 41 |
|
| | 42 | s = deblank(s);
|
| | 43 | % Remove any commas so that numbers formatted like 1,200.34 are
|
| | 44 | % handled.
|
| | 45 | s(s == ',')= [];
|
| | 46 | lenS = numel(s); %get len again since it has changed after deblanking
|
| | 47 |
|
| | 48 | % Try to get 123, 123i, 123i + 45, or 123i - 45
|
| | 49 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[ij] %1[+-] %f',4);
|
| | 50 | % simlest case is a double
|
| | 51 | if count == 1 && isempty(errmsg) && nextindex > lenS
|
| | 52 | x = a;
|
| | 53 | return;
|
| | 54 | end
|
| | 55 | % now deal with complex
|
| | 56 | if isempty(errmsg) && nextindex > lenS
|
| | 57 | if count==2
|
| | 58 | x = a(1)*1i;
|
| | 59 | elseif count==4
|
| | 60 | sign = (a(3)=='+')*2 - 1;
|
| | 61 | x = complex(sign*a(4), a(1));
|
| | 62 | else
|
| | 63 | x = NaN;
|
| | 64 | end
|
| | 65 | return
|
| | 66 | end
|
| | 67 |
|
| | 68 | % Try to get 123 + 23i or 123 - 23i
|
| | 69 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[+-] %f %1[ij]',4);
|
| | 70 | if isempty(errmsg) && nextindex > lenS
|
| | 71 | if count==4
|
| | 72 | sign = (a(2)=='+')*2 - 1;
|
| | 73 | x = complex(a(1), sign*a(3));
|
| | 74 | else
|
| | 75 | x = NaN;
|
| | 76 | end
|
| | 77 | return
|
| | 78 | end
|
| | 79 |
|
| | 80 | % Try to get i, i + 45, or i - 45
|
| | 81 | [a,count,errmsg,nextindex] = sscanf(s,' %1[ij] %1[+-] %f',3);
|
| | 82 | if isempty(errmsg) && nextindex > lenS
|
| | 83 | if count==1
|
| | 84 | x = 1i;
|
| | 85 | elseif count==3
|
| | 86 | sign = (a(2)=='+')*2 - 1;
|
| | 87 | x = complex(sign*a(3), 1);
|
| | 88 | else
|
| | 89 | x = NaN;
|
| | 90 | end
|
| | 91 | return
|
| | 92 | end
|
| | 93 |
|
| | 94 | % Try to get 123 + i or 123 - i
|
| | 95 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[+-] %1[ij]',3);
|
| | 96 | if isempty(errmsg) && nextindex > lenS
|
| | 97 | if count==1
|
| | 98 | x = a(1);
|
| | 99 | elseif count==3
|
| | 100 | sign = (a(2)=='+')*2 - 1;
|
| | 101 | x = complex(a(1), sign);
|
| | 102 | else
|
| | 103 | x = NaN;
|
| | 104 | end
|
| | 105 | return
|
| | 106 | end
|
| | 107 |
|
| | 108 | % Try to get -i, -i + 45, or -i - 45
|
| | 109 | [a,count,errmsg,nextindex] = sscanf(s,' %1[+-] %1[ij] %1[+-] %f',4);
|
| | 110 | if isempty(errmsg) && nextindex > lenS
|
| | 111 | if count==2
|
| | 112 | sign = (a(1)=='+')*2 - 1;
|
| | 113 | x = sign*1i;
|
| | 114 | elseif count==4
|
| | 115 | sign1 = (a(1)=='+')*2 - 1;
|
| | 116 | sign2 = (a(3)=='+')*2 - 1;
|
| | 117 | x = complex(sign2*a(4), sign1);
|
| | 118 | else
|
| | 119 | x = NaN;
|
| | 120 | end
|
| | 121 | return
|
| | 122 | end
|
| | 123 |
|
| | 124 | % Try to get 123 + 23*i or 123 - 23*i
|
| | 125 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[+-] %f %1[*] %1[ij]',5);
|
| | 126 | if isempty(errmsg) && nextindex > lenS
|
| | 127 | if count==5
|
| | 128 | sign = (a(2)=='+')*2 - 1;
|
| | 129 | x = complex(a(1), sign*a(3));
|
| | 130 | else
|
| | 131 | x = NaN;
|
| | 132 | end
|
| | 133 | return
|
| | 134 | end
|
| | 135 |
|
| | 136 | % Try to get 123*i, 123*i + 45, or 123*i - 45
|
| | 137 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[*] %1[ij] %1[+-] %f',5);
|
| | 138 | if isempty(errmsg) && nextindex > lenS
|
| | 139 | if count==1
|
| | 140 | x = a;
|
| | 141 | elseif count==3
|
| | 142 | x = a(1)*1i;
|
| | 143 | elseif count==5
|
| | 144 | sign = (a(4)=='+')*2 - 1;
|
| | 145 | x = complex(sign*a(5), a(1));
|
| | 146 | else
|
| | 147 | x = NaN;
|
| | 148 | end
|
| | 149 | return
|
| | 150 | end
|
| | 151 |
|
| | 152 | % Try to get i*123 + 45 or i*123 - 45
|
| | 153 | [a,count,errmsg,nextindex] = sscanf(s,' %1[ij] %1[*] %f %1[+-] %f',5);
|
| | 154 | if isempty(errmsg) && nextindex > lenS
|
| | 155 | if count==1
|
| | 156 | x = 1i;
|
| | 157 | elseif count==3
|
| | 158 | x = 1i*a(3);
|
| | 159 | elseif count==5
|
| | 160 | sign = (a(4)=='+')*2 - 1;
|
| | 161 | x = complex(sign*a(5), a(3));
|
| | 162 | else
|
| | 163 | x = NaN;
|
| | 164 | end
|
| | 165 | return
|
| | 166 | end
|
| | 167 |
|
| | 168 | % Try to get -i*123 + 45 or -i*123 - 45
|
| | 169 | [a,count,errmsg,nextindex] = sscanf(s,' %1[+-] %1[ij] %1[*] %f %1[+-] %f',6);
|
| | 170 | if isempty(errmsg) && nextindex > lenS
|
| | 171 | if count==2
|
| | 172 | sign = (a(1)=='+')*2 - 1;
|
| | 173 | x = sign*1i;
|
| | 174 | elseif count==4
|
| | 175 | sign = (a(1)=='+')*2 - 1;
|
| | 176 | x = sign*1i*a(4);
|
| | 177 | elseif count==6
|
| | 178 | sign1 = (a(1)=='+')*2 - 1;
|
| | 179 | sign2 = (a(5)=='+')*2 - 1;
|
| | 180 | x = complex(sign2*a(6), sign1*a(4));
|
| | 181 | else
|
| | 182 | x = NaN;
|
| | 183 | end
|
| | 184 | return
|
| | 185 | end
|
| | 186 |
|
| | 187 | % Try to get 123 + i*45 or 123 - i*45
|
| | 188 | [a,count,errmsg,nextindex] = sscanf(s,'%f %1[+-] %1[ij] %1[*] %f',5);
|
| | 189 | if isempty(errmsg) && nextindex > lenS
|
| | 190 | if count==5
|
| | 191 | sign = (a(2)=='+')*2 - 1;
|
| | 192 | x = complex(a(1), sign*a(5));
|
| | 193 | else
|
| | 194 | x = NaN;
|
| | 195 | end
|
| | 196 | return
|
| | 197 | end
|
| | 198 |
|
| | 199 | % None of the above cases, but s still is a character array.
|
| | 200 | x = NaN;
|
| | 201 |
|
| | 202 | elseif ~isempty(s) && iscellstr(s)
|
| | 203 | x = cellfun(@str2double, s);
|
| | 204 | elseif iscell(s)
|
| | 205 | x = textArrayType2double(s, @cellLoop);
|
| | 206 | elseif isstring(s)
|
| | 207 | x = textArrayType2double(s, @stringLoop);
|
| | 208 | else
|
| | 209 | x = NaN;
|
| | 210 | end
|
| | 211 |
|
| | 212 | end
|
Other subfunctions in this file are not included in this listing.