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.

In the SIP2NUM and BIP2NUM documentation the word string refers to a 1xN character vector, and not to the MATLAB string class.

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 =
                3450000000
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 =
                  23000000                     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 either the prefix symbol or the full prefix name. The optional second input argument can be used to force SIP2NUM to only match only the prefix symbol or only the prefix name:

[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

For convenience SIP2NUM identifies three different "micro" characters:

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

Space Character

For convenience SIP2NUM identifies many different space characters, .e.g:

sip2num(sprintf('1\x0020u')) % (U+0020) 'SPACE'
sip2num(sprintf('1\x00A0u')) % (U+00A0) 'NO-BREAK SPACE'
sip2num(sprintf('1\x2007u')) % (U+2007) 'FIGURE SPACE'
sip2num(sprintf('1\x202Fu')) % (U+202F) 'NARROW NO-BREAK SPACE'
sip2num(sprintf('1\x205Fu')) % (U+205F) 'MEDIUM MATHEMATICAL SPACE'
ans =
                     1e-06
ans =
                     1e-06
ans =
                     1e-06
ans =
                     1e-06
ans =
                     1e-06

Bonus: BIP2NUM Binary Prefix Function

The submission includes the bonus function BIP2NUM: this converts a string with prefixes to numeric values, with ISO 80000 specified binary prefixes instead of metric prefixes. Binary prefixes are 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 =
                  1022.976
num =
                  1025.024
spl = 
    ''    ''
sgf =
     4

Bonus: RKM2NUM RKM-Code Function

The submission includes the bonus function RKM2NUM: this converts a string with prefixes to numeric values, with IEC 60062 specified RKM code instead of metric prefixes. RKM code is used for resistors and capacitors.

rkm2num('1R2')
rkm2num('4k7')
ans =
                       1.2
ans =
        4700

Reverse Conversion: Numeric to String

The functions NUM2SIP and NUM2BIP and NUM2RKM convert from numeric into string:

num2bip(1280)
num2sip(1250)
num2rkm(1250)
ans =
1.25 Ki
ans =
1.25 k
ans =
1k25