Scientific Prefix to Number

versión 4.0.1 (18.3 KB) por Stephen23
Convert SI-prefixed text (aka engineering / metric prefix) into numeric values. Bonus: binary prefixes!

588 descargas

Actualizada 29 Nov 2022

Ver licencia

The function SIP2NUM converts a string with an SI prefix (aka metric prefix, or engineering prefix) into a numeric value. 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 many 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 restricted to either a 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 and prefixes.
  • Returns the number of significant figures detected in the coefficients.
  • Includes the prefixes added in 2022: ronna, quetta, ronto, and quecto.
Reverse Conversion
To convert from numeric to prefixed string:
SI Prefix Examples
>> sip2num('10 k') % OR sip2num('10.0 kilo') OR sip2num('10000') OR sip2num('1e4')
ans = 10000
>> [num,spl] = sip2num("Power: 200 megawatt")
num = 200000000
spl = ["Power: ","watt"]
>> [num,spl,sgf] = sip2num("from -3.6 MV to +1.24kV potential difference.")
num = [-3600000,1240]
spl = ["from ","V to ","V potential difference."]
sgf = [2,3]
>> [num,spl] = sip2num("100 meter","meter") % Try it without the second option.
num = 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
>> [num,spl] = bip2num("Memory: 200 mebibyte")
num = 209715200
spl = ["Memory: ","byte"]
>> [num,spl,sgf] = bip2num("From -3.6 MiB to +1.24KiB data allowance.")
num = [-3774873.6,1269.76]
spl = ["From ","B to ","B data allowance."]
sgf = [2,3]
>> [num,spl] = bip2num("100 Pixel","Pixel") % Try it without the second option.
num = 100
spl = ["","Pixel"]
>> bip2num(num2bip(pow2(9,40))) % 9 tebi == pow2(9,40) == 9*1024^4
ans = 9895604649984
SI Prefixes (Bureau International des Poids et Mesures)
Magnitude | Symbol | Name
1000^-10 | q | quecto
1000^-9 | r | ronto
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
1000^+9 | R | ronna
1000^+10 | Q | quetta
Binary Prefixes (IEC 60027-2 A.2 and ISO/IEC 80000-13:2008)
Magnitude | Symbol | Name
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').

Citar como

Stephen23 (2022). Scientific Prefix to Number (https://www.mathworks.com/matlabcentral/fileexchange/53886-scientific-prefix-to-number), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2010b
Compatible con la versión R2009b y siguientes
Compatibilidad con las plataformas
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!