File Exchange

## Simple Units and Dimensions for Matlab

version 1.0 (5.51 KB) by

This code provides an effecient, easy to use, method of incorporating real world units ...

Updated

***Note***
This method of using units is quick and clean. For a more complete version that automatically does dimensional analysis, see the file entitled "Units and Dimensions Suite for Matlab" (See the Acknowledgements section below)
*******

The units.m function returns a struct containing the SI values of many common units. The self-contained help demonstrates how to use this struct to cleanly and effeciently perform matlab calculations using commonly encountered units and physical constants. The code is easily modified to include any non-standard unit or constant desired. This method for handling units was inspired by the methods used by MathCAD.

Patrik Eschle

### Patrik Eschle (view profile)

Simple and robust, thank you.

I use it in parallel with physunits, that provides more sophisticated units handling, but is more complex and thus more error prone.

Why oh why is this not a Matlab native feature?

Richard Stephens

### Richard Stephens (view profile)

This is such a simple and elegant way of handling units. I love it!

Rob deCarvalho

Check out the improved way of handling units. Following the suggestions below, I wrote an improved version of units.m which incorporates a matlab class to both display units and enforce dimensional consistency throughout your application. It should post by 2/23/06. Check it out and please let me know off errors/improvements

John D'Errico

Improved now with self contained help. I'd still prefer to see an implementation as a class, but I also recognize this would take a significant amount of work to implement.

Nathaniel Brahms

This file is very useful, and makes entry and conversion of units much simpler than more cumbersome "conversion" programs.

A note on use (this should be shown in the hlep file):
To enter a number in a given unit, MULTIPLY by the unit:
l = 5*u.in;
To display a calculation in a given unit, DIVIDE by the unit:
l/u.m
To convert a number between two units, MULTIPLY by the starting unit, and DIVIDE by the ending unit:
5*u.in/u.m
To enter complicated units, just multiply:
g = 9.8*u.m/u.s^2;

Also, pressure is force divided by area, not times.

Rob deCarvalho

The code was updated on 2/6/05 to include self contained help. The example file has been removed in favor of including it in the self-contained help.
A significant improvement to this code could be achieved by creating a matlab class that would overload all math operations and check the expressions for unit consistency. It could also display results in any desired unit. This would, however, result in significantly increased complexity to ensure bug-free operation. In its current form, units.m is compact and very simple to incorporate into more complicated calculations.

John D'Errico

The example file is reasonably short. It absolutely should be merged into the help for units itself. As it is, the function units has NO self-contained help. An example function "unitsExample" is poor matlab programming style. If I am running Matlab and need to remember how to use units, how should I remember that the help for it is in the function unitsExample.m? This biased my rating lower than it might have been. Some might argue my rating should be lower still. Its easily fixed though, and I hope/assume the author will choose to repair this serious flaw so I will leave the rating as a 3.
Units itself is a simple, clean tool that does work as the author claims. A second reason I did not rate it higher is because part of me thinks this application does cry out for a slicker tool that will display the units automatically. I'd like to see something that will see you multiply force times area, and understand the result is a pressure and display the proper units with no thought required on the part of the user.
I'll note that this was created using R14 SP3, but it has no properties that prevent it from being used in much older releases, as long as they allow the use of structures.