5.0

5.0 | 7 ratings Rate this file 34 Downloads (last 30 days) File Size: 105.88 KB File ID: #29621

Units Conversion Toolbox

by John McDermid

 

04 Dec 2010 (Updated 25 Jan 2012)

This toolbox attaches units to Matlab variables and enables unit conversion.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Have you ever needed to convert horse power to watts but can't remember the conversion factor? How many inches light travels in a microsecond? This toolbox is designed to make getting these answers much easier.

The "unit" statement attaches units to a variable. For example,
>> x=unit(2,'yards')
 
x =
     1.8288 m

returns the answer in International (SI) units. If you would rather have you answer in feet it can be converted to feet by

>> convert(x,'feet')
 
ans =
     6 feet

Variables created by the "unit" statement can operated on by these operators:

      Unary plus, Unary minus, +,-,*,.*,/,\,./,.\,^,.^,',.'
      and sqrt

Variables are also supported by triginometric,hyperbolic, and exponential functions and can be compared by

       ==, ~=, >, <, >=, and <=

Special plotting routines are available for plotting variables with units. These are:

      plot, semilogx, semilogy, and loglog

The toolbox appears in the html help screens and has "GettingStarted', "User Guide", and function help screens.

Just down load the tool box to the directory of you choice and set the Matlab path to include this directory. To verify operation, type

>> unit('yards')
 
ans =
     0.9144 m

If you get this result, you are all set!

Acknowledgements
This submission has inspired the following:
Unit Converters
MATLAB release MATLAB 7.11 (2010b)
Other requirements none
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
09 Sep 2011 Erich

In MATLAB R2011b I get:

>> Units(3, 'ft')
Error using Units
Error: File: Units.m Line: 1 Column: 10
Naming a class 'Units' is not allowed, because it is already a built-in class.

21 Sep 2011 Erich

Very easy to work with units using this toolbox. Author quickly updated the files when R2011b introduced an incompatibility.

16 Nov 2011 John

This is a great toolbox to have handy but it seems to have problems with vectors and arrays of objects. For example, (I am running MATLAB 2010b):

>> a = [unit(1,'m') unit(2,'m')]
 
a =
??? Error using ==> plus
Too many input arguments.

Error in ==> unit>unit.display at 1827
                s=sprintf(' %g %s',x.value+x.offset,x.name);

But when I change it to

>> a = [unit(1,'m') unit(2,'m')];

there is not issue. It seems the call to sprintf is having trouble with multiple inputs.
Overall this printing issue is not what concerns me, it is when I try to perform an operation on this array when I really run into trouble:

>> a * unit(1,'m')
??? Comma separated list must have exactly one item.

Error in ==> unit>unit.unit at 163
                    if U.hasBeenConverted

Error in ==> unit>unit.mtimes at 387
                A=unit(A);B=unit(B);

I was hoping to get back a <1x2 unit> where both values are now in m^2. I run some highly vectorized code and it was be great if this issue could be resolved, overall great toolbox. Thanks

17 Nov 2011 Michael Katz  
18 Nov 2011 Erich

Try
>> a= unit([1 2],'m')

21 Nov 2011 John

Thanks Erich, that works perfectly

18 Jan 2012 Collin

Great toolbox.

Ran into a small problem with non-dimensional unit's plus and minus (unit class with .name = ''). I fixed the problem by replacing all the

  if A.name == B.name
        ......
  end

with

    if strcmpi(A.name,B.name)
         ......
    end

as ''=='' is an empty matrix

18 Jan 2012 Collin  
02 Apr 2012 Ronan CIMADURE

Perfect :)

30 Apr 2012 Marco

can you add this function to the unit class? Some formulas has absolute value in them, and I need to preserve units.

function C=abs(A)
A=unit(A);
C=A; C.value=abs(A.value);
end

30 Apr 2012 Marco

I cannot use this toolbox to find roots of polynomial using the roots function when coefficientes are unit-class type. I first have to define a isfinite function in the unit class, but then I have problems with the transpose and ctranspose function I am unable to solve myself. Can someone help?

Please login to add a comment or rating.
Updates
16 Sep 2011

This program has been updated to resolve a problem that appeared in R2011b.
R2011B used "Units" as a built in function in a toolbox and does not check to see if you have the toolbox before throwing an error. "Units" has been renamed to "unit".

21 Sep 2011

This update was submitted and approved but did not end up in Matlab Central. Instead, the old one was there.

21 Sep 2011

I made an omission on the last submission and no file (or the old file) was attached. This is the correct file!

25 Jan 2012

This update corrects the addition and subtraction of dimensionless quantities (previously reported an error) and also corrects the definition of a weber.

Tag Activity for this File
Tag Applied By Date/Time
units John McDermid 06 Dec 2010 08:29:45
conversion John McDermid 06 Dec 2010 08:29:45
units Matthew 27 Oct 2011 14:07:07
potw Lindsay Coutinho 03 Feb 2012 15:58:14
pick of the week Lindsay Coutinho 03 Feb 2012 15:58:15

Contact us at files@mathworks.com