MATLAB Examples

SIP2NUM and BIP2NUM Examples

The function SIP2NUM converts a string containing a number with a metric prefix into a numeric value, for example '1 k' -> 1000. The function is based on REGEXP, and 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 m')
sip2num('3.45 Giga')
sip2num('6.7 kV and 89 mOhm')
ans =
                    0.0012
ans =
                3450000000
ans =
                      6700                     0.089

Output Argument: Split Strings

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

[num,spl] = sip2num('I applied 23 mV to the device, for 5 ms.')
num =
                     0.023                     0.005
spl = 
    'I applied '    'V to the device, for '    's.'

Output Argument: 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

Optional 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. Simply input 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'

Optional Input: Symbol or Full Name

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

[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
num =
                       0.1
spl = 
    ''    'illi'
num =
                       0.1
spl = 
    ''    ''
num =
   100
spl = 
    ''    'm'

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 =
                  1022.976
num =
                  1025.024
spl = 
    ''    ''
sgf =
     4

Reverse Conversion: Numeric to String

The functions NUM2SIP and NUM2BIP convert from numeric scalars into SI prefixed and binary prefixed strings.