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.
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.
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.
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.
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.
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]
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:
Variable = value * u.unit
Variable = [unit] value
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
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.rad = 1;
u.cycle = 2*pi*u.rad;
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
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]
One comment: I changed
u.kt = 1.15077945*u.mph;
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;
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.
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.