File Exchange

image thumbnail

Scientific Prefix to Number

version 3.2.1 (17.9 KB) by Stephen Cobeldick
Convert a metric prefixed string into numeric value (SI/engineering). Bonus: binary prefixes!

6 Downloads

Updated 10 Jul 2020

View License

The function SIP2NUM converts a string with a metric prefix (aka SI prefix, or engineering prefix) into numeric values. For example '1 k' is converted to 1000.

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 numbers including +/- sign, decimal digits and exponent E-notation.
* Detects zero or more numbers in the string.
* Returns the parts of the input string that are split by the detected number+prefix.
* Returns the number of significant figures detected in the numbers.

### Bonus Functions ###

The function BIP2NUM converts from binary-prefixed string to numeric, for example '1 Ki' is converted to 1024.

The function RKM2NUM converts from RKM code to numeric, for example '1k2' is converted to 1200.

### 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(9e12)) % 9 tera == 9e12 == 9*1000^4
ans = 9000000000000

### 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 allowance.')
val = [-3774873.6,1269.76]
spl = {'From ','B to ','B data allowance.'}
sgf = [2,3]

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

>> bip2num(num2bip(pow2(9,40))) % 9 tebi == pow2(9,40) == 9*1024^4
ans = 9895604649984

### 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 | µ / 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').
* accept different "micro" symbol characters: U+0075, or U+00B5, or U+03BC,

Cite As

Stephen Cobeldick (2020). Scientific Prefix to Number (https://www.mathworks.com/matlabcentral/fileexchange/53886-scientific-prefix-to-number), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Harry Dymond

Thanks for taking my suggestion on board and implementing the change so quickly :-)

@HarryDymond: that is a very good idea. I just added support for the characters you specified, can you please test it again.

Harry Dymond

Please could you add support for the micro symbol (U+00B5)/greek mu (U+03BC), and the true minus character (U+2212)?

ER

Updates

3.2.1

* Add error IDs.

3.2.0

* Case-insensitive matching of INF and NaN (just like SSCANF etc).
* Add bonus function RKM2NUM.

3.1.3

* More test cases.
* More robust whitespace handling.

3.1.2

* Update documentation.
* Improve significant figure measurement.

3.1.1

* Improve documentation.
* Improve significant figure counting.

3.1.0

* Parse micro characters U+00B5 & U+03BC.
* Parse negative character U+2212.

3.0.1

* Improve documentation.

3.0.0.0

* Simplify algorithm: faster!

2.1.0.0

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

1.1.0.0

* Update FEX examples.

MATLAB Release Compatibility
Created with R2010b
Compatible with R2009b to any release
Platform Compatibility
Windows macOS Linux