time | Calls | line |
---|
| | 1 | function [x,ok] = str2num(s)
|
| | 2 | %STR2NUM Convert character array or string scalar to numeric array
|
| | 3 | % X = STR2NUM(S) converts a character array or string scalar
|
| | 4 | % representation of a matrix of numbers to a numeric matrix. For example,
|
| | 5 | %
|
| | 6 | % S = ['1 2' str2num(S) => [1 2;3 4]
|
| | 7 | % '3 4']
|
| | 8 | %
|
| | 9 | % The numbers in S should be text representations of a numeric values.
|
| | 10 | % Each number may contain digits, a decimal point, a leading + or - sign,
|
| | 11 | % an 'e' or 'd' preceding a power of 10 scale factor, and an 'i' or 'j' for
|
| | 12 | % a complex unit.
|
| | 13 | %
|
| | 14 | % If S does not represent a valid number or matrix, STR2NUM(S) returns
|
| | 15 | % the empty matrix. [X,OK]=STR2NUM(S) returns OK=0 if the conversion fails.
|
| | 16 | %
|
| | 17 | % CAUTION: STR2NUM uses EVAL to convert the input argument, so side
|
| | 18 | % effects can occur if S contains calls to functions. Use
|
| | 19 | % STR2DOUBLE to avoid such side effects or when S contains a single
|
| | 20 | % number.
|
| | 21 | %
|
| | 22 | % Also spaces can be significant. For instance, str2num('1+2i') and
|
| | 23 | % str2num('1 + 2i') produce x = 1+2i while str2num('1 +2i') produces
|
| | 24 | % x = [1 2i]. These problems are also avoided when you use STR2DOUBLE.
|
| | 25 | %
|
| | 26 | % See also STR2DOUBLE, NUM2STR, HEX2NUM, CHAR.
|
| | 27 |
|
| | 28 | % Copyright 1984-2016 The MathWorks, Inc.
|
| | 29 |
|
< 0.001 | 4 | 30 | if nargin > 0
|
< 0.001 | 4 | 31 | s = convertStringsToChars(s);
|
< 0.001 | 4 | 32 | end
|
| | 33 |
|
< 0.001 | 4 | 34 | if ~ischar(s) || ~ismatrix(s)
|
| | 35 | error(message('MATLAB:str2num:InvalidArgument'))
|
< 0.001 | 4 | 36 | end
|
| | 37 |
|
< 0.001 | 4 | 38 | if isempty(s)
|
| | 39 | x = [];
|
| | 40 | ok=false;
|
| | 41 | return
|
< 0.001 | 4 | 42 | end
|
| | 43 |
|
| | 44 | % Replace any char(0) characters with spaces
|
< 0.001 | 4 | 45 | s(s==char(0)) = ' ';
|
| | 46 |
|
< 0.001 | 4 | 47 | [m,n] = size(s);
|
< 0.001 | 4 | 48 | if m==1,
|
< 0.001 | 4 | 49 | [x,ok] = protected_conversion(['[' s ']']); % Always add brackets
|
| | 50 | else
|
| | 51 | semi = ';';
|
| | 52 | space = ' ';
|
| | 53 | if ~any(any(s == '[' | s == ']')), % String does not contain brackets
|
| | 54 | o = ones(m-1,1);
|
| | 55 | s = [['[';space(o)] s [semi(o) space(o);' ]']]';
|
| | 56 | elseif ~any(any(s(1:m-1,:) == semi)), % No ;'s in non-last rows
|
| | 57 | s = [s,[semi(ones(m-1,1));space]]';
|
| | 58 | else % Put ;'s where appropriate
|
| | 59 | spost = space(ones(m,1));
|
| | 60 | for i = 1:m-1,
|
| | 61 | last = find(s(i,:) ~= space,1,'last');
|
| | 62 | if s(i,n-last+1) ~= semi,
|
| | 63 | spost(i) = semi;
|
| | 64 | end
|
| | 65 | end
|
| | 66 | s = [s,spost]';
|
| | 67 | end
|
| | 68 | [x,ok] = protected_conversion(s);
|
< 0.001 | 4 | 69 | end
|
< 0.001 | 4 | 70 | if isnumeric(x)
|
< 0.001 | 4 | 71 | return
|
| | 72 | end
|
| | 73 | if ischar(x) || iscell(x) || isstring(x)
|
| | 74 | x = [];
|
| | 75 | ok = false;
|
| | 76 | end
|
Other subfunctions in this file are not included in this listing.