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.
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?
This is such a simple and elegant way of handling units. I love it!
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
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.
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:
To convert a number between two units, MULTIPLY by the starting unit, and DIVIDE by the ending unit:
To enter complicated units, just multiply:
g = 9.8*u.m/u.s^2;
Also, pressure is force divided by area, not times.
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.
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.
Updating the file description to reference the improved version
Updating the description and title to this post.
Example file has been moved into the help section and is no longer included as a separate file.
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.