SIP2NUM and BIP2NUM Examples

The function SIP2NUM converts a character vector (containing a number with optional metric prefix) into a numeric, for example '1 k' -> 1000. The function can detect and convert multiple numbers in the string, both with and without metric prefixes. SIP2NUM returns the numeric values, the string parts split by the detected numbers (and prefixes), and the detected number of significant digits in for each detected number.

The development of SIP2NUM was motivated by the lack of any well- written function that provides this conversion: many of the functions available on FEX do not conform to the SI standard, or use buggy conversion algorithms, or are simply painfully slow. SIP2NUM has been tested against a large set of test cases, including many edge-cases and all of the optional arguments.

Contents

Basic Usage

In many cases SIP2NUM can be called with just a string value:

sip2num('1.2 k')
sip2num('3.45 Giga')
sip2num('6.7 µV and 89 mOhm')
ans =
        1200
ans =
     3.45e+09
ans =
      6.7e-06        0.089

2nd Output: Split Strings

SIP2NUM also returns the string parts split by the detected numbers:

[num,spl] = sip2num('I applied 23 MV for 5 ms.')
num =
      2.3e+07        0.005
spl = 
    'I applied '    'V for '    's.'

3rd Output: Significant Digits

SIP2NUM returns the significant digits of the detected numbers:

[num,~,sgf] = sip2num('987.6 kV or 0.99 MV ?')
num =
      987600      990000
sgf =
     4     2

2nd Input: Specify Units

Some units may be mistaken for prefixes, in which case the second input argument can be used to force the units to be identified as such. Optionally supply the units string and these units will be ignored:

[num,spl] = sip2num('100 meter') % 'm' is falsely identified as a prefix
[num,spl] = sip2num('100 meter','meter') % specify the units to get the correct value
num =
          0.1
spl = 
    ''    'eter'
num =
   100
spl = 
    ''    'meter'

2nd Input: Symbol or Full Name

By default SIP2NUM will try to match both the prefix symbol or the full prefix name. The optional second input argument can be used to force SIP2NUM to only match one of these (note that just the number or symbol character may still be matched by SIP2NUM):

[num,spl] = sip2num('100 milli',false) % match symbol only
[num,spl] = sip2num('100 milli',true)  % match prefix name only
[num,spl] = sip2num('100 m',true) % cannot match name -> matches number only
num =
          0.1
spl = 
    ''    'illi'
num =
          0.1
spl = 
    ''    ''
num =
   100
spl = 
    ''    'm'

Micro Symbol

SIP2NUM identifies three kinds of "micro" symbol:

sip2num('1 u') % ASCII        (U+0075) 'LATIN SMALL LETTER U'
sip2num('1 µ') % ISO 8859-1   (U+00B5) 'MICRO SIGN'
sip2num(sprintf('1 \xB5'))  % (U+00B5) 'MICRO SIGN'
sip2num(sprintf('1 \x3BC')) % (U+03BC) 'GREEK SMALL LETTER MU'
ans =
        1e-06
ans =
        1e-06
ans =
        1e-06
ans =
        1e-06

BIP2NUM (Binary Prefix)

The submission includes the bonus function BIP2NUM: this also converts a string with prefixes to numeric values, but uses the ISO 80000 defined binary prefixes instead of metric prefixes. Binary prefixes are mostly used for computer memory.

The function BIP2NUM has exactly the same arguments as SIP2NUM:

bip2num('0.999 kibi')
[num,spl,sgf] = bip2num('1.001 kibi',true)
ans =
         1023
num =
         1025
spl = 
    ''    ''
sgf =
     4

Reverse Conversion: Numeric to String

The functions NUM2SIP and NUM2BIP convert from numeric into prefixed strings:

num2sip(1e7)
num2bip(pow2(10,20))
ans =
10 M
ans =
10 Mi