home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / STRFUN.DI$ / STR2NUM.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  3.8 KB  |  140 lines

  1. function x = str2num(s)
  2. %STR2NUM String to number conversion.
  3. %    X = STR2NUM(S)  converts the string S, which should be an
  4. %    ASCII character representation of a numeric value, to MATLAB's
  5. %    numeric representation.  The string may contain digits, a decimal
  6. %    point, a leading + or - sign, an 'e' preceeding a power of 10 scale
  7. %    factor, and an 'i' for a complex unit.
  8. %    If the string S does not represent a valid number, STR2NUM(S)
  9. %    returns the empty matrix.
  10. %
  11. %    See also NUM2STR, HEX2NUM.
  12.  
  13. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  14.  
  15. if ~isstr(s) | size(s,1) > 1
  16.    error('Requires string argument.')
  17. end
  18. % remove trailing and leading blanks
  19. s = deblank(fliplr(deblank(fliplr(s))));
  20. if isempty(s),
  21.    x = [];
  22.    return
  23. end
  24. % find remaining blanks
  25. ib = find(s == ' ');
  26. if ~isempty(ib)
  27.    ib1 = diff(ib);
  28.    if isempty(ib1) % only 1 blank - make sure it's before or after a sign
  29.       if ~any(s([ib-1 ib+1]) == '+' | s([ib-1 ib+1]) == '-')
  30.          x = [];
  31.          return
  32.       end
  33.    else % look for contiguous blocks of 1s, at most 2 of them - i.e., at most 1 non-1
  34.       ib11 = find(ib1 ~= 1);
  35.       if length(ib11) > 1
  36.          x = [];
  37.          return
  38.       elseif length(ib11) == 1 % make sure the character in between is a plus or a minus
  39.          ipm = any(s([ib-1 ib+1]) == '+' | s([ib-1 ib+1]) == '-');
  40.          if ~ipm
  41.             x = []
  42.             return
  43.          end
  44.       end
  45.    end
  46. end
  47. % now squeeze out remaining blanks
  48. s(s==' ') = [];
  49. % check for unwanted characters
  50. if ~all (((s < 58) & (s > 47)) | (s == 'i') | (s == 'j') | ...
  51.    (s == '.') | (s == 'e') | (s == 'E') | (s == '+') | (s == '-') | (s == '.'))
  52.    x = [];
  53.    return
  54. end
  55. % Peel off possible imaginary part.
  56. % it better be the last thing, if it exists at all.
  57. % possible complex number - try to convert.
  58. ind = find(s == 'j');
  59. if ~isempty(ind)
  60.    s(ind) = 'i';
  61. end
  62. ind = find(s == 'i');
  63. if ~isempty(ind)
  64.    if length(ind) > 1
  65.       x = [];
  66.       return
  67.    end
  68.    if ind ~= length(s)
  69.       x = [];
  70.       return
  71.    end
  72.    if ind == 1
  73.       s = ['+0+1i'];
  74.    end
  75. % make sure there is a real part and separate real and imaginary parts with
  76. % a space for sscanf.
  77. % to help, if there's no leading sign, put in a plus
  78.    if s(1) ~= '+' & s(1) ~= '-'
  79.       s = ['+' s];
  80.    end
  81. % look for character before + and - signs; the real and imaginary parts are
  82. % separated with + or - signs and optional spaces, but before the +/-, there
  83. % must be a number
  84.    sind = find(s=='+' | s=='-');
  85.    if isempty(sind)  % number had better be purely imaginary
  86.       s = ['+0+' s];
  87.    else
  88. % ignore leading sign for now
  89.       sind(1) = [];
  90. % find potential digits or periods preceding a sign (otherwise it's e or malformed)
  91.       digits = s(sind-1);
  92.       dig_ind = find(digits == '.' | (abs(digits) >= 48 & abs(digits) <= 57));
  93.       if length(dig_ind) ~= 1
  94.          x = [];
  95.          return
  96.       end
  97.       s = [s(1:sind(dig_ind)-1) ' ' s(sind(dig_ind):length(s))];
  98.    end
  99. % pulled off final i, now read 2 parts
  100. % check to make sure there's a digit just before this
  101.    s(length(s)) = [];
  102.    last = s(length(s));
  103.    if abs(last) < 48 | abs(last) > 57 
  104.       if last ~= '.'
  105.          s = [s '1'];
  106.       end
  107.    end
  108.    [a,b,c] = sscanf([s ' '],'%g',2);
  109.    if isempty(c)
  110.       x = a(1)+i*a(2);
  111.    else
  112.       x = [];
  113.    end
  114.    return
  115. else
  116.    % check to make sure that there is only one number in the string if 
  117.    %   there is no 'e' or 'E' in it
  118.    if ~any(s=='e' | s == 'E'),
  119.       [a,b,c] = sscanf([s ' '],'%g',2);
  120.       if b > 1
  121.          x = [];
  122.          return;
  123.       end
  124.    end
  125. end
  126.  
  127. % make sure that the string does not end in an 'e' or an 'E'
  128. if (s(length(s)) == 'e') | (s(length(s)) == 'E'),
  129.     x = [];
  130.     return
  131. end
  132.  
  133. % possible real number - try to convert.
  134. [a,b,c] = sscanf([s ' '],'%g',1);
  135. if isempty(c)
  136.    x = a;
  137. else
  138.    x = [];
  139. end
  140.