File Exchange

image thumbnail


version (23.4 KB) by Mark Mikofski
Quantities is an units and uncertainties package for MATLAB.

1 Download

Updated 17 Aug 2015

From GitHub

View Version History

View license on GitHub

Quantities is an units and uncertainties package for MATLAB.
Clone or download the Quantities package to your MATLAB folder as `+Quantities`.

1. Construct a units registry, which contains all units, constants, prefixes and

>> ureg = Quantities.unitRegistry

ureg =

Map with properties:

Count: 279
KeyType: char
ValueType: any

2. Optionally pass `verbosity` parameter to `unitRegistry` to see list of units

>> ureg = Quantities.unitRegistry('v',2)

3. Units and constants can be indexed from the `unitRegsitry` using their name
or alias. The `unit`, `constant` and `quantity` class all subclass to
`double` so you can perform any operation on them. Combining a `double` with
a `unit` creates a `quantity` class object.

>> T1 = 45*ureg('celsius')
T1 =
45 ± 0 [degC];

>> T2 = 123.3*ureg.degC
T2 =
123.3 ± 0 [degC];

4. Perform operations. All units are converted to base.

>> T2.to_base
ans =
396.45 ± 0 [kelvin];

>> heat_loss = ureg.stefan_boltzmann_constant*(T1^4 - T2^4)
heat_loss =
-819814 ± 0 [gram*second^-3];

>> heat_loss = ureg.stefan_boltzmann_constant*(T1.to_base^4 - T2.to_base^4)
heat_loss =
-819814 ± 0 [gram*second^-3];

5. Add uncertainty to quantities by calling constructor.

>> T3 = Quantities.quantity(56.2, 1.23, ureg.degC)
T3 =
56.2 ± 1.23 [degC];

>> heat_loss = ureg.stefan_boltzmann_constant*(T1^4 - T3^4)
heat_loss =
-86228.1 ± 9966.66 [gram*second^-3];

6. Convert output to different units.

>> heat_loss_kg = heat_loss.convert(^3)
heat_loss_kg =
-819.814 ± 0 [kilogram*second^-3];

7. Determine arbitrary conversion factor.

>> conversion_factor = ureg.mile.convert(
conversion_factor =
1.60934 ± 0 [kilometer];

Cite As

Mark Mikofski (2021). mikofski/Quantities (, GitHub. Retrieved .

Comments and Ratings (9)


Very useful
but,It's long time to load(ureg = Quantities.unitRegistry;my computer needs 8.6s)

Mark Mikofski

Added some documentation in the wiki -

Mark Mikofski

fixed 2 issues
* no average property exception if converting to exact same units
* max recursion limit exception if dividing units by quantity
* bumped up to v0.4.1, Captain America (beta) Release (commit <59882fb>)

Mark Mikofski

Errata! Fixed units to and from reversed in conversion factory. Please update to latest commit <239e6ee> (v1.3 on MATLAB FEX). The previous commit <14c1042e> had the to and from units reversed in the conversion factor, eg: converting from grams to kg yielded 1000[kg] = 1[g] instead of 1000[g] = 1[kg]. Sorry for any inconvenience. Also updated README to show convert() method usage. Please report any bugs on GitHub at Thanks!

Mark Mikofski

Note: the newest version probably won't be available until tomorrow or monday, since TMW only scrapes GitHub once daily. Of course you can download a tarball straight from the GitHub releases page:

Mark Mikofski

@Xiadong You can now convert to units with matching dimensions using Quantities.quantity.convert method. So if you are using [gauss] and then perform operations, call the convert(ureg.gauss) on the result of your calculations and it should return your value in [gauss]:

>> emf = 10*ureg('gauss')
>> V1 = 3e-6*ureg.volt
>> dt = 1.23e-3*ureg('sec')
>> a = 4.56*ureg('mm')^2
>> emf_V1 = V1*dt/a
emf_V1 =
0.809211 ± 0 [coulomb^-1*gram*second^-1];
>> emf_V1.convert(ureg.gauss)
ans =
0.0809211 ± 0 [gauss];
>> r = emf+emf_V1
r =
1.80921 ± 0 [coulomb^-1*gram*second^-1];
>> r.convert(ureg('gauss'))
ans =
0.180921 ± 0 [gauss];
>> r.convert(ureg.tesla)
ans =
1809.21 ± 0 [tesla];
>> ureg.gauss.convert(ureg.tesla)
ans =
10000 ± 0 [tesla];

Hope this helps!


I am not sure if you can automatically transfer Gauss units to SI units and vice verse?

Mark Mikofski

Not seeing something you wished was there? Feel free to create an issue on GitHub, fork the repo, hack it yourself and I'll gladly accept your PR or just comment here. Let me know what features you want/need. Documentation is on the way.

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!