File Exchange

image thumbnail

Scientific Prefix to Number

version 3.0 (9.63 KB) by

Convert a metric prefixed string into numeric value (SI/engineering). Bonus: binary prefixes!

6 Downloads

Updated

View License

The function SIP2NUM converts a string with a metric prefix (aka SI prefix, or engineering prefix) into numeric values. For example the string '1 k' is converted to 1000. The bonus function BIP2NUM converts from Binary-prefixed string to numeric, for example the value '1 Ki' is converted to 1024.

After testing the metric-prefix submissions on MATLAB FEX (see Acknowledgements) and not finding a single one that converted all values correctly, I wrote my own functions. And then exhaustively tested them to confirm that they actually give the correct output.

This submission:
* Automatically detects the prefix, or it may be specified as being either the name or symbol.
* Detects coefficients including +/- sign, decimal digits and exponent E-notation.
* Detects zero or more coefficients in the string.
* Returns the parts of the input string that are split by the detected coefficients & prefixes.
* Returns the number of significant figures detected in the coefficients.

### Reverse Conversion ###

To convert from numeric to metric-prefixed string:

<http://www.mathworks.com/matlabcentral/fileexchange/33174-number-to-scientific-prefix>

### Metric Prefix Examples ###

sip2num('10 k') OR sip2num('10.0 kilo') OR sip2num('10000') OR sip2num('1e4')
ans = 10000

[val,spl] = sip2num('Power: 200 megawatt')
val = 200000000
spl = {'Power: ','watt'}

[val,spl,sgf] = sip2num('from -3.6 MV to +1.24kV potential difference.')
val = [-3600000,1240]
spl = {'from ','V to ','V potential difference.'}
sgf = [2,3]

[val,spl] = sip2num('100 meter','meter') % Try it without the second option.
val = 100
spl = {'','meter'}

sip2num(num2sip(9*1000^4))
ans = 9000000000000 % == 9*1000^4

### Binary Prefix Examples ###

bip2num('10 Ki') OR bip2num('10.0 kibi') OR bip2num('10240') OR bip2num('1.024e4')
ans = 10240

[val,spl] = bip2num('Memory: 200 mebibyte')
val = 209715200
spl = {'Memory: ','byte'}

[val,spl,sgf] = bip2num('From -3.6 MiB to +1.24KiB data transfer.')
val = [-3774873.6,1269.8]
spl = {'From ','B to ','B data transfer.'}
sgf = [2,3]

[val,spl] = bip2num('100 Pixel','Pixel') % Try it without the second option.
val = 100
spl = {'','Pixel'}

bip2num(num2bip(9*1024^4))
ans = 9895604649984 % == 9*1024^4

### Metric Prefixes ###

Magnitude | Symbol / Name
1000^-8 | y / yocto
1000^-7 | z / zepto
1000^-6 | a / atto
1000^-5 | f / femto
1000^-4 | p / pico
1000^-3 | n / nano
1000^-2 | u / micro
1000^-1 | m / milli
1000^0 |
1000^1 | k / kilo
1000^2 | M / mega
1000^3 | G / giga
1000^4 | T / tera
1000^5 | P / peta
1000^6 | E / exa
1000^7 | Z / zetta
1000^8 | Y / yotta

### Binary Prefixes (IEC 60027-2 A.2 and ISO/IEC 80000-13:2008) ###

Magnitude | Symbol / Name
1024^0 |
1024^1 | Ki / kibi
1024^2 | Mi / mebi
1024^3 | Gi / gibi
1024^4 | Ti / tebi
1024^5 | Pi / pebi
1024^6 | Ei / exbi
1024^7 | Zi / zebi
1024^8 | Yi / yobi

### Notes ###

These functions have been extensively tested against many edge cases, with particular attention to ensuring the correct handling of exponential notation. Compared to similar submissions available on MATLAB File Exchange, these functions correctly:
* parse negative strings (try '-1').
* parse any combination of E-notation values (try '1e0', '1e0 k', '1e30').

Comments and Ratings (0)

Updates

3.0

* Simplify algorithm: faster!

2.1

* Change second input true/false to match NUM2SIP and NUM2BIP.
* Add HTML documentation.

1.1

* Update FEX examples.

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video