File Exchange

image thumbnail

mikofski/Quantities

version 1.3 (395 KB) by

Quantities is an units and uncertainties package for MATLAB.

0 Downloads

Updated

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

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

>> 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
loaded.

>> 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(ureg.kg/ureg.s^3)
heat_loss_kg =
-819.814 ± 0 [kilogram*second^-3];

7. Determine arbitrary conversion factor.

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

Comments and Ratings (8)

Mark Mikofski

Mark Mikofski (view profile)

Added some documentation in the wiki - https://github.com/mikofski/Quantities/wiki

Mark Mikofski

Mark Mikofski (view profile)

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

Mark Mikofski (view profile)

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 https://github.com/mikofski/Quantities/issues. Thanks!

Mark Mikofski

Mark Mikofski (view profile)

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:

https://github.com/mikofski/Quantities/releases/tag/v0.4

Mark Mikofski

Mark Mikofski (view profile)

@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!

Xiaodong

Xiaodong (view profile)

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

Mark Mikofski

Mark Mikofski (view profile)

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.

Updates

1.2

add package name to unitRegistry calls

1.1

add readme

1.3

* fix to_base() method to convert units with nested base units, eg: gauss -> tesla -> weber/m^2
* implement convert() methods for unit and quantity classes

MATLAB Release
MATLAB 8.5 (R2015a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today