### Highlights from Units and Dimensions Suite for Matlab

4.83333
4.8 | 6 ratings Rate this file 15 Downloads (last 30 days) File Size: 19.1 KB File ID: #10070 Version: 1.0

# Units and Dimensions Suite for Matlab

### Rob deCarvalho (view profile)

20 Feb 2006 (Updated )

A complete suite for using real world units in matlab.

File Information
Description

The units.m function provides a complete methodology of incorporating real world units into matlab calculations. Units.m returns a struct with fields containing a new class of variable called a dimensioned variable. A dimensioned variable contains a value and a dimension. The mathematical operations performed on these variables are designed to ensure that your code remains dimensionally consistent throughout your calculation. For example, an error occurs if you try to add a time variable to a length variable.

***note****
A quick and clean method of handling units was previously posted with the title "Simple Units and Dimensions for Matlab". Although this previous method did not force dimensional consistency, it may be a better choice for applications where simplicity is desired.
******

Acknowledgements

Simple Units And Dimensions For Matlab inspired this file.

This file inspired Units For Matlab and Dimensioned Variable Class Units Support For Matlab.

MATLAB release MATLAB 7.1.0 (R14SP3)
01 Feb 2013 David Smith

### David Smith (view profile)

08 Nov 2012 Sky Sartorius

### Sky Sartorius (view profile)

I use this function all the time. I was trained in imperial units but now work in Germany - this thing is awesome. Really reduces errors, makes code easier to understand, and speeds up my work.

12 Sep 2012 Sky Sartorius

### Sky Sartorius (view profile)

I like this quite a bit. I had to add a lot of my own units for my specific field, e.g. nautical miles, slugs, psf, and for the kitchen (tsp, Tbls). I look forward to having mature units capability in MATLAB one day, and this is a good first step. Maybe Mathworks will take the reigns, if we're lucky, as units are a capability that Matlab's competitors DO have.

Comment only
27 Mar 2012 Frank Dekens

### Frank Dekens (view profile)

I would also prefer the format proposed by Greg, with a minor tweak of having the units after the value. so
mass = 1.0 [kg]
I don't even mind the * sign, or even the u. (since those would be easy to replace afterwards). So something like:
mass = 1.0 * [u.kg]
It would also be nice to drop the ^1, since that is the standard power and isn't really needed.
Thanks, FGD

10 Feb 2012 Craig

### Craig (view profile)

This looks very promising for what I want, but is there a way to modify the way the program displays so that the name of the variable is displayed? When I don't use a semicolon to suppress display I really want to see the Variable as well as the value and units. Right now I only see the units and the value.
I would like something like:

Variable = value * u.unit
=>
Variable = [unit]
value

While I'm at it I would also love to create an option to change the display to 1 line. Ideally this would be enabled and disabled the way format is used. For example:

unit_display one_line
Variable = value * u.unit
=>
Variable = [unit] value

Comment only
01 Sep 2011 Sergio Rossi

### Sergio Rossi (view profile)

Excellent utility! Helped me find a nasty error in my calculation. Vectors/matrices where all elements have the same units work flawlessly. Thanks very much

28 Jan 2011 Steven

### Steven (view profile)

Excellent utility. Handling units this way (even with the "simple" version) greatly improves the readability of code. Code like this could prevent spaceships from crashing into Mars!

One possible modification: I've found some calculations in electronics work out more cleanly if you express all frequencies as rad/sec:
u.Hz = u.cycle / u.sec;

Then you can say things like:
w_cutoff = 1/(R*C); % RC filter cutoff frequency
w_cutoff / u.Hz % display it in Hertz, if necessary

Comment only
24 Nov 2010 John Kitchin

### John Kitchin (view profile)

Nice work! This is a big improvement over the simple units file with regards to maintaining consistency of units.

One problem I immediately ran into using this class is making a vector of numbers with different units. This happens all the time when solving coupled differential equations where the initial conditions might be [1*u.mol/L 300*u.K]. The simple units file allowed this easily (of course because no checks are made!)

there could also be linear algebra cases where it is appropriate to have different units in two different vectors, but where the element-wise product all have the same units.

Some other issues:
this does not work because horzcat is only defined for 2 arguments.
a = [1*u.m 2*u.m 3*u.m]

Comment only
01 Jul 2010 Erich

### Erich (view profile)

Great utility.

One comment: I changed
u.kt = 1.15077945*u.mph;
to
u.kt = 1.852*u.km/u.hr;
in units.m, as this is the exact definition for knots.

I also modified lbf, slug, and slinch so that 1 pound force is exactly slug*ft/s^2:
% Can't use u.g because it isn't defined yet.
u.lbf = u.lbm * 9.80665*u.m/u.s^2;
% Now define slug and slinch (mass)
u.slug = u.lbf*u.s^2/u.ft;
u.slin = 12*u.slug;

10 Mar 2007 tom de smet

The implementation, obviously, is error prone due to the large amount of cut-and-paste work which was needed during development. Otherwise, the class works PERFECTLY well, and it will definitely improve as more people use it & report issues found.

One bug (I think) is in mtimes.m, case 'only V1 is a dim var', where V2*V1 should be V1*V2 imho.

21 Feb 2006 John D'Errico

Right now, this appears to be a work with room for improvement. Interested users might want to subscribe to this tool (as I have done) to be notified when improvements are made. I've explicitly chosen to NOT provide a rating immediately for that reason. Its clearly something the author has put a lot of effort into, and deserves patience. I've verified so far that some simple operations do work properly.
Currently, this tool desperately needs help in the function DimensionedVariable. It was not at all obvious how to call and use it. A ReadMe file with some additional demos might help too.

Comment only