Code covered by the BSD License

### Highlights from DimensionedVariable Class - units support for MATLAB

5.0

5.0 | 2 ratings Rate this file 22 Downloads (last 30 days) File Size: 33.7 KB File ID: #38977

# DimensionedVariable Class - units support for MATLAB

09 Nov 2012 (Updated )

A new variable type allowing use of real-world units and dimensions with enforced unit consistency.

File Information
Description

A DimensionedVariable attaches real-world units (any combination of mass, length, time, etc.) to otherwise normal numeric variables. Math operations performed on DimensionedVariables will automatically perform dimensional analysis to ensure that units are consistent.

In my experience this tool has helped me reduce units-related errors, has made my code much easier to understand, and has also generally sped up my work. It is also very handy for day-to-day unit conversions and other small tasks.

Recent changes:
-Easily switch to using a different base unit system and work in any base unit system you like, e.g. English units (default is still SI).
-Added fuller functionality to several methods.
-Sped up and/or simplified several methods, some significantly.

Rob is responsible for the first iterations of this submission: http://www.mathworks.com/matlabcentral/fileexchange/10070

Acknowledgements
MATLAB release MATLAB 8.1 (R2013a)
16 May 2014

@Emanuele: I expanded size and the other oversimplified methods to support their full functionality. Thanks for inspiring me to go through and clean up and improve everything.

The "content" method you describe is the same as the existing "u2num" method.

13 May 2014

Great tool. Two minor changes for improving interoperability with existing code:

function outSize = size(v1,v2)
outSize = size(v1.value);
if nargin == 2
outSize = outSize(v2);
end

function y = content(x)
y = x.value
end

13 Feb 2014

In order to do what you describe, Thomas, u2num and unitsOf can't be normal methods of the class (at least not by themselves). I see three solutions to do what you want:

1) Make them static methods; add
isa(inVariable,'DimensionedVariable')
...
else
out = in;
end
This required writing "DimensionedVariable." before all function calls.
2) Move the unitsOf and u2num m files from the @DimensionedVariable folder to somewhere on your path (I suggest the same folder where units.m lives); add the same if/else as above.
3) Don't bother with "if isa(..." - just make a function on your path like
function [out] = u2num( varargin )
out = varargin{1};
end
MATLAB will use this for any input that isn't a DimensionedVariable.

I don't find any of the above solutions to be very elegant, which is why I haven't done any of them for the released submission. Perhaps someone here can suggest a more elegant solution.

P.S. I added the ability to switch base unit systems (e.g. use feet and lbs instead of meters and kg). Stay tuned for a new upload.

05 Feb 2014

Thanks for the great routine!
I have one suggestion: when the result of a computation is a dimensionless variable, it will be a double. But when using u2num() or unitsOf() on a double, the algorithm fails. Therefore I need to check the variables before batch processing them (for automatic printing in Latex using siunitx package and newcommands).
Could you include something like
if strcmp(class(val),'DimensionedVariable')
...
else if strcmp(class(val),'double')
...
in those functions?
Regards Thomas

12 Apr 2013

I uploaded a new version that, among other improvements, especially in documentation, has an entirely new subsasgn file that should take care of the removing elements problem and the preallocating problem. When doing subscripted assignment, you have to be assigning using input/output with the same units. The only exception is that NaN and [] are now allowed (either normal or with units). For preallocation, use a dimensioned NaN array. If you don't know the dimensions you'll need, the new subsasgn will allow changing the units of an all-NaN array to match the assigned variable (with a warning).

12 Apr 2013

Very cool, I use it extensively and it really helps.
One thing that I encountered is that assignements such as vect(index)=[] which remove elements in a vector don't work when using a DimensionedVariable vector.
Also preallocating arrays e.g. before a loop is not possible when we don't know the unit of the content at the moment of allocation. Maybe NaN and 0 should be treated specially, in a sense these have no unit and any unit at the same time.

04 Dec 2012

added many units and did a lot of checking an updating so that more units meet their exact definitions

22 Jan 2013

13 Mar 2013

14 Apr 2013

new subsasgn method; new html documentation; reworked help blocks; other updates

16 Jul 2013

New display method looks much better (in response to comments on rob's original post); new classdef structure

19 Jul 2013

Fixed bug in displaying units with no numerator

29 Aug 2013

Added new static method 'unitslist' that makes it easy to drastically speed up code developed using the class.

09 Sep 2013

Improved unitslist; added kp force unit.

24 Sep 2013

30 Oct 2013

added pressure, angle, and angular velocity units; added static method for working with string representations of units.

21 Mar 2014

Added ability to switch base unit systems; added many more units; added 'dummy' unitsOf and u2num functions to keep from crashing when calling with non-DimensionedVariable arguments (addresses user concern)

19 May 2014

New, simpler, and fuller-functioning methods.