Code covered by the BSD License  

Highlights from
DIMENSION - tagging values with units

5.0

5.0 | 1 rating Rate this file 5 Downloads (last 30 days) File Size: 32.59 KB File ID: #6850

DIMENSION - tagging values with units

by Nathan Thern

 

02 Feb 2005 (Updated 07 Feb 2005)

Class for tagging values with units. Provides for transparent conversion, unit checking & combining.

| Watch this File

File Information
Description

Joe engineer's script works in radians and meters. The lab data files are in degrees and inches and the drawings are in mm. Joe tags everything with dimensions and never worries again about conversions. He even discovers a bug where a value he thought represented area really represents distance.

Whirlwind Tour:
 >> side = dimension(500,'m')
 side = 500 m
 >> height = dimension(2.1,'km');
 >> height + side
 ans = 2.6 km
 >> side + height
 ans = 2600 m
 >> height > dimension(1,'mi')
 ans = 1
 >> convert(side*height,'acre')
 ans = 259.461 acre
 >> msquared = dimension(1,'m',2)
 msquared = 1 m^2
 >> convert(side*height,msquared)
 ans = 1050000 m^2
 >> speed = dimension(60,'mi','hr',-1);
 >> value(speed)
 ans = 60
Or
 >> speed.value
 ans = 60
 >> convert(speed,'kph')
 ans = 96.5606 kph
 >> ubase(speed)
 ans = 26.8224 m s^-1
 >> ubase(dimension(1,'btu'))
 ans = 1055.06 kg m^2 s^-2
 >> convert(height,'kg')
 ??? Error ==> units are incompatable
 >> height + speed
 ??? Error ==> units are incompatable
 >> speed * dimension(1.5,'hr')
 ans = 90 mi
 >> thickness = dimension([1,2;3,4],'in');
 thickness = 1 2
               3 4 in
 >> thickness(2,1)
 ans = 3 in
 >> [ thickness [ side/1000; height/1000 ] ]
 ans = 1 2 19.685
         3 4 82.677 in
 >> thickness'
 ans = 1 3
         2 4 in
 >> angle = dimension(30,'deg');
 >> tan(angle)
 ans = 0.57735
Any value can be used as an argument to DIMENSION:
 >> i_time = dimension(1*i,'s');
Users can define their own units based on previously defined units or redefine existing units:
 >> frlng = dimension(220,'yd');
 >> add_unit(frlng,'furlong');
 >> convert(height,'furlong')
 ans = 10.439 furlong
 >> add_base(dimension, 'beaut');
 >> add_unit(dimension(.75,'beaut','joule',-1),'tnt');
 >> ubase(dimension(700,'tnt'))
 ans = 525 beaut s^2 kg^-1 m^-2

Some defined units:
s, sec, min, hr, dy, wk, yr, mon, millenium
m, cm, mm, a, km, ft, in, mil, yd, mi, nm, ly, au, parsec
r, radian, deg, arcmin, arcsec, grad, cycle, rev
kg, gm, mg, tonne, lbm, oz, slug, ton, amu
k, kelvin, rankine
amp, ma
mole, mol
bit, byte, kB, MB, GB, TB
cd, candle
dollar, $, cent
cc, l, ml, gal, qt, floz
kph, knot, mph, c
Hz, KHz, MHz, GHz, rpm
nt, dyne, lb, lbf
joule, erg, cal, btu, ev, mev, kwh
sr, coulomb, ftlb, watt, gray, farad, ohm, mho, v, henry, tesla, gauss, wb

This is beta software and still in the bug-killing stage. Everything shown above works correctly, though.

MATLAB release MATLAB 6.5.1 (R13SP1)
Other requirements Only tested so far on 6.5.1. Uses classes and overloading. Probably will work or can be modified to work down to ML5; I can't see any reason why not, but haven't tested it yet.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
06 Feb 2005 - -

how does this compare to umat class?

08 Feb 2005 The Author

I guess the primary difference from umat is that you can find it with a search on "units" or "dimensions" :-)
I had no idea umat even existed, and will take a close look at it.

27 Jun 2005 Jock Christie

This is a great program.
Very very helpful.

Only comment is that it is (sometimes) overly rigid about unit names.
It should treat watt=Watt=Watts the same.

Please login to add a comment or rating.
Updates
07 Feb 2005

Left one file out of the zip

Tag Activity for this File
Tag Applied By Date/Time
chemistry Nathan Thern 22 Oct 2008 07:40:33
physics Nathan Thern 22 Oct 2008 07:40:33
units Nathan Thern 11 Feb 2011 18:50:43
unit Nathan Thern 11 Feb 2011 18:50:43
unit conversion Nathan Thern 11 Feb 2011 18:50:43
dimensions Nathan Thern 11 Feb 2011 18:50:43
dimension Nathan Thern 11 Feb 2011 18:50:43
dimension Ronan CIMADURE 02 Apr 2012 01:35:08

Contact us at files@mathworks.com