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