Douglas B. Staple
Douglas B. Staple

Reputation: 10946

What are the valid formats for numbers in MATLAB?

What are the valid formats are for numbers in MATLAB? The following seem to be valid:

x=0;
x=0.;
x=0.0;
x=0e0;
x=0E0;
x=0000.00; % Trailing and leading zeros seem to be irrelevant

Are there other valid general number specifications? I can't find this in the documentation.

Upvotes: 3

Views: 75

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

I believe this is the regex of floating-point number formats, valid in MATLAB:

^[-+]*([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)([eEdD][+-]?[0-9]+)?$

Compiled from here, and slightly modified for MATLAB:

  • added 'd' exponent character (as is common in FORTRAN, MATLAB's ancestor)
  • added uppercase exponent characters
  • added extra case in the required order before and after the decimal symbol

I'm pretty sure the locale can mess this up, e.g., the decimal separator . might be set to , as is common here in Europe. Oh well.

The regex in words:

  • string start, followed by
  • zero or more consecutive sign symbols, followed by
    • non-zero length string of consecutive integers, OR
    • possibly zero-length string of consecutive integers, followed by a dot, followed by non-zero length string of consecutive integers, OR
    • non-zero length string of consecutive integers, followed by a dot, followed by a possibly zero-length string of consecutive integers
  • optionally followed by the exponent part:
    • one of e, E, d or D.
    • zero or one sign symbols, followed by
    • non-zero length string of consecutive integers
  • followed by string terminator

Note that this is for non-complex floating point values. For complex values, you'd have to

  • use the regex once for the real, once for the imaginary part
  • append [ij]{1} to the imaginary part (only lower case)
  • take care of spacing (\s*) and a [+-]{1} in between the two parts
  • take care of the fact that the imaginary part may appear alone, but the real part may not appear with a trailing [+-]{1}, but no imaginary part.

Upvotes: 4

Related Questions