Get from Ico-github-logo

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

» Watch video

Highlights from
mikofski/Quantities

  • version
    VERSION Tag and name of this release.
  • quantity.m
  • constant
  • dimension
  • prefix
  • unit
  • unitRegistry
  • testscript.m
    tests
  • View all files
Be the first to rate this file! 1 Download (last 30 days) File Size: 395 KB File ID: #48988 Version: 1.3
image thumbnail

mikofski/Quantities

by

Mark Mikofski (view profile)

 

09 Jan 2015 (Updated )

Quantities is an units and uncertainties package for MATLAB.

| Watch this File

File Information
Description

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];

MATLAB release MATLAB 8.5 (R2015a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
01 Jun 2015 Mark Mikofski

Mark Mikofski (view profile)

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

Comment only
01 Jun 2015 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>)

Comment only
01 Jun 2015 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!

Comment only
29 May 2015 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

Comment only
29 May 2015 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!

Comment only
22 Apr 2015 Xiaodong

Xiaodong (view profile)

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

Comment only
12 Jan 2015 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.

Comment only
Updates
17 Aug 2015 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

18 Aug 2015 1.1

add readme

18 Aug 2015 1.2

add package name to unitRegistry calls

Contact us