Code covered by the BSD License  

Highlights from
Dimensioned variable class - units support for MATLAB

5.0 | 2 ratings Rate this file 10 Downloads (last 30 days) File Size: 83.7 KB File ID: #38977 Version: 2.02
image thumbnail

Dimensioned variable class - units support for MATLAB


Sky Sartorius (view profile)


09 Nov 2012 (Updated )

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

| Watch this File

File Information

A DimVar (Dimensioned Variable) object attaches real-world units (any combination of mass, length, time, etc.) to otherwise normal numeric variables. Math operations performed on dimensioned variables will automatically perform dimensional analysis to ensure that units are consistent.
This tool has helped me reduce units-related errors, has made my code much easier to understand, and has also generally sped up my workflow. It is also very handy for day-to-day unit conversions and other small tasks.
Lots of features to help your workflow, increase clarity, and reduce errors:
-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)
-Plot automatically labels axes with units
-Other methods for intuitive, clear display: disp, num2str, and others
-Custom display units, e.g. to display "W" instead of "[m^2][kg]/s^3"
-Conversions to/from new Matlab duration class
-Additional u.m classdef lets you access dimensioned variables everywhere (similar to other constant function like pi or i)
-Automatic dynamic scaling
-And much more


Units And Dimensions Suite For Matlab inspired this file.

This file inspired Simple Turbine Engine Performance Estimation, Stdatmo: Standard Atmosphere Function, and Fuel Fraction Sizing.

Required Products MATLAB
MATLAB release MATLAB 8.6 (R2015b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
25 Feb 2015 Sky Sartorius

Sky Sartorius (view profile)

That sounds interesting. Contact me via my profile.

Comment only
24 Feb 2015 Thomas

Thomas (view profile)

Thank you again, I have just refactored my code to take advantage of your alterations! Would you be interested in code that translates the units() objects into formated strings for use in siunitx latex package?
It is able to generate prefixes for orders of magnitude (e.g. mega,micro), takes into account units that do not take prefixes (kelvin) and knows about significant digits.

Comment only
10 Feb 2015 Sky Sartorius

Sky Sartorius (view profile)

Download the latest version, which has unitsOf and u2num implemented for non-DimVars as well. It also has a documented way to expand the list of base units (e.g., add 'mol') either by editing your units.m file or programmatically by providing the units function with your own unit system.

Comment only
10 Feb 2015 Thomas

Thomas (view profile)

Thanks for the response. Number 3 perfectly fixes my issue!
How about realizing 1 and 3? Add a u2num and unitsOf function to the units folder that contains the units() function and the class definition folder. Such that you can simply add the units folder to the path and it simply works out of the box.

Another one: how about adding support for mole?
Are you developing using git?

Comment only
16 May 2014 Sky Sartorius

Sky Sartorius (view profile)

@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.

Comment only
13 May 2014 Emanuele Ruffaldi

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);

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

13 Feb 2014 Sky Sartorius

Sky Sartorius (view profile)

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
out = in;
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};
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.

Comment only
05 Feb 2014 Thomas

Thomas (view profile)

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

Comment only
12 Apr 2013 Sky Sartorius

Sky Sartorius (view profile)

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).

Comment only
12 Apr 2013 John Doe

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 1.2

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

22 Jan 2013 1.3

added some units

13 Mar 2013 1.4

Documentation and examples; added functionality to u2num; added meshgrid function

14 Apr 2013 1.6

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

16 Jul 2013 1.8

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

19 Jul 2013 1.10

Fixed bug in displaying units with no numerator

29 Aug 2013 1.11

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

09 Sep 2013 1.12

Improved unitslist; added kp force unit.

24 Sep 2013 1.13

added features to unitslist method

30 Oct 2013 1.14

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

21 Mar 2014 1.15

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 1.16

New, simpler, and fuller-functioning methods.

03 Dec 2014 1.17

Class name change!
Methods added: disp, num2str, cumtrapz, numel, isempty, reshape.
Plot method with auto-labeling axes with units.
A few more units added.

05 Jun 2015 1.18

Fixed error with Rankine.

21 Aug 2015 2.01

Preferred display units, updated documentation, some new methods.

16 Oct 2015 2.02

Plot method updated for R2015b, added isfinite and validateattributes methods, added u classdef functionality.

Contact us