4.88889

4.9 | 10 ratings Rate this file 42 Downloads (last 30 days) File Size: 106 KB File ID: #29621

Units Conversion Toolbox

by

 

04 Dec 2010 (Updated )

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 file inspired Unit Converters.

MATLAB release MATLAB 7.11 (R2010b)
Other requirements none
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
18 Aug 2014 Nick Dutton

Hi,

I noticed that the conversion from eV to Joules is incorrect.

Line 104 of convConstant.m should read:

"y=units2convFac(1.60217646e-19*unit('J'))"

Besides this one error, very nice utility!

05 Nov 2013 Sinan

There is an error using units with offset.

test = unit('C');
answer = 2*(test+test);

answer should give result of
1096.6 = 2*(274.15+274.15);

I implemented the following to avoid this error.

In classdef unit in function U=unit(varargin) ...
switch nargin
...
case 2
%find this line

if isa(varargin{1},'double') && isa(varargin{2},'char')
X=unit(varargin{2});
U=X;
if U.hasBeenConverted == false
U.value=varargin{1}*X.value+X.offset;
U.offset = 0;
else
U.value=varargin{1}*X.value;
end

I hope this does not interfer with other functionalities, but my verifications showed no strange behaviour anymore.

Many thanks for your tool!
Best regards
Sinan

24 Aug 2012 Matthew M.

I've been thinking about incorporating this for a while, hemming and hawing. Tried it today, finally, and I'm sold. Great job.

I wish there were a way to match the display to the Command Window format setting - I generally like to display more significant digits than short g allows. But it appears that there's no easy way to retrieve the current command window display format (I'll bet Yair Altman could figure one out, but I couldn't). I know you can use the .value method to display it in the desired format, but that's sort of inelegant.

The workaround for me was to edit unit\display directly, changing the sprintf format from %g to %0.12g (I chose 12 rather arbitrarily).

Thanks!

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?

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

02 Apr 2012 Ronan CIMADURE

Perfect :)

18 Jan 2012 Collin  
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

21 Nov 2011 John

Thanks Erich, that works perfectly

18 Nov 2011 Erich

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

17 Nov 2011 Michael Katz  
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

21 Sep 2011 Erich

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

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.

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.

Contact us