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

Defining Variables and their Units

Defining Variables and their Units

Contents

Fundamental Units

A "unit" statement accepts a number and a unit name and converts these to international units (SI). There are just seven different units in the international system of units. These are:

         seconds (s)    -- units of time
         meters (m)     -- units of length
         kilograms (kg) -- units of mass
         ampere (A)     -- units of electrical current
         kelvin (K)     -- units of temperature
         mole (mol)     -- units of substance quantity
         candela (cd)   -- units of light intensity

The parenthesis contain the abreviation for the fundamental unit. Abbreviations are used as the unit names. These names may appear as a simple abbreviation such as when specifying a yard.

unit(1,'yard')
 
ans = 
     0.9144 m

or as a mathematical combination such as when specifying an ohm.

unit(1,'ohm')
 
ans = 
     1 m^2*kg/s^3/A^2

Finding Defined Unit Names

The available units and their names can be found with the "unit.search" command. To see if there is a unit for pounds, search for "pound" with the search command.

unit.search('pound')
     Units of weight in pounds (avoirdupois)     {'pound','pounds','lb','lbs','lbm'}
     Units of pounds force     {'lbf'}

Two choices are presented, one for pounds avoirdupois and one for pounds force. The search command returns all descriptions that contain "pound". If you would like to see the entire table of constants, you can use the word "all".

unit.search('all')
     Units of time in seconds     {'seconds','second','sec','s'}
     Units of length in meters     {'meter','meters','m'}
     Units of mass in kilograms     {'kg'}
     Units of electrical current in amperes     {'ampere','amperes','amp','amps','A'}
     Units of temperature in degrees kelvin     {'kelvin','K'}
     Units of amount of a substance in moles     {'mole','moles','mol'}
     Units of light intensity in candelas     {'candela','candelas','cd'}
     Units of force in newtons     {'newton','newtons','N'}
     Units of pressure in pascals     {'pascal','pascals','Pa'}
     Units of energy in joules     {'joule','joules','J'}
     Units of power in watts     {'watt','watts','W'}
     Units of electrical charge in coulombs     {'coulomb','coulombs','C'}
     Units of voltage in volts     {'volt','volts','V'}
     Units of magnetic flux in webers     {'weber','webers'}
     Units of magnetic flux density in teslas     {'tesla','teslas'}
     Units of magnetic flux density in gauss     {'gauss'}
     Units of capacitance in farads     {'farad','farads','F'}
     Units of resistance in ohms     {'ohm','ohms'}
     Units of inductance in henrys     {'henry','henrys','H'}
     Units of focal length in diopters     {'diopter','diopters'}
     Units of length in centimeters     {'cm'}
     Units of weight in grams     {'gram','grams','gr'}
     Units of energy in calories     {'calorie','calories','cal'}
     Units of energy in kilocalories     {'Calorie','Calories','kcal'}
     Units of energy in dynes     {'dyne','dynes'}
     Units of energy in british thermal units     {'Btu','BTU'}
     Units of energy in electron volts     {'eV'}
     Units of power in horse power     {'hp'}
     Units of length in kilometers     {'km'}
     Units of length in yards     {'yard','yards','yd','yds'}
     Units of length in feet     {'foot','feet','ft'}
     Units of length in inches     {'inch','inches','in'}
     Units of length in miles     {'mile','miles'}
     Units of length in fathoms     {'fathom','fathoms'}
     Units of length in rods     {'rod','rods'}
     Units of length in furlongs     {'furlong','furlongs'}
     Units of length in nautical miles     {'nmi','naut_mi'}
     Units of length in leagues     {'league','leagues'}
     Units of length in angstroms     {'angstrom'}
     Units of length in light years     {'lightyear'}
     Units of length in astronomical units     {'au'}
     Units of distance that a star appears to shift when the earth moves one "au"     {'parsec','parsecs'}
     Units of time in minutes     {'minute','minutes','min'}
     Units of time in hours     {'hour','hours','hr'}
     Units of time in days     {'day','days'}
     Units of time in weeks     {'week','weeks'}
     Units of time in fortnights     {'fortnight','fortnights'}
     Units of time in years     {'year','years','yr'}
     Units of time in centuries     {'century','centuries'}
     Units of weight in pounds (avoirdupois)     {'pound','pounds','lb','lbs','lbm'}
     Units of weight in ounces (avoirdupois)     {'ounce','ounces','oz'}
     Units of weight in grains     {'grain'}
     Units of weight in tons     {'ton'}
     Units of atomic mass units     {'amu','u'}
     Units of pounds force     {'lbf'}
     Units of temperature in degrees celsius     {'celsius','degC'}
     Units of temperature in degrees fahrenheit     {'degF'}
     Units of temperature in degrees rankine     {'degR'}
     Units of area in acres     {'acre','acres'}
     Units of area in "are" (pronounced "air")     {'are'}
     Units of area in hectares     {'hectare','hectares'}
     Units of area in barns     {'barn','barns'}
     Units of volume in liters     {'liter','liters','L'}
     Units of volume in cubic centimeters     {'cc'}
     Units of volume in fluid ounces     {'fldoz'}
     Units of volume in table spoons     {'tablespoon','tablespoons','tblsp'}
     Units of volume in tea spoons     {'teaspoon','teaspoons','tsp'}
     Units of volume in cups     {'cup','cups'}
     Units of volume in pints     {'pint','pints'}
     Units of volume in quarts     {'quart','quarts'}
     Units of volume in gallons (u.s.)     {'gallon','gallons'}
     Units of volume in acre feet     {'acre_foot','acre_feet'}
     Units of velocity in miles per hour     {'mph'}
     Units of velocity in mach numbers (speed of sound)     {'mach'}
     Units of velocity in nautical miles per hour     {'knot','knots','kn'}
     Units of the speed of light in a vacuum     {'c'}
     Units of acceleration due to gravity     {'g'}
     Units of charge on an electron     {'e'}
     Units of mass of an electron     {'me'}
     Units of mass of a proton     {'mp'}
     Units of energy expended over time     {'Planck','h'}
     Units of fine structure constant     {'alpha'}
     Units of gas constant     {'R'}
     Units of avogadro's number     {'Avogadro','NA'}
     Units of magnetic permeability of free space     {'muZero'}
     Units of dielectric constant of free space     {'epsilonZero'}
     Units of pressure in atmospheres     {'atm'}
     Units of pressure in torr     {'torr'}
     Units of pressure in bars     {'bar','bars'}
     Units of angle in radians     {'radian','radians','rad'}
     Units of angle in degrees     {'degree','degrees','deg'}
     Units of a periodic unit     {'cycle','cycles','rev'}
     Units of frequency in hertz     {'hertz','Hz'}

Creating New Units from Existing Unit Names

The existing unit names will never cover all of the instances of unit names you may require. For example, the conversion table contains no name for the velocity in feet per second. However, it is possible to construct a unit name through mathematical operators. The ballistic table for the 0.17 Remington Fireball cartridge shows the velocity to be

Velocity=unit([4250 3594 3028 2529 2081],'feet/second');

where the velocity is expressed in 100 yard increments from the muzzle out to 400 yards. The velocity unit name is defined as the ratio of units "feet" and "second".

The operands available to combine defined units are *, /, and ^. Addition and subtraction are not permitted with unit names. The number 1 is permitted so that reciprocals can be formed. For example

unit('1/yd')
 
ans = 
     1.09361 1/m

Converting to an Equivalent Unit of Measure

The "unit" statement always expresses the result in the SI system of measure. Units can be converted to any equivalent unit of measure with the "convert" statement. The velocity, defined above, can be expressed in mach numbers.

convert(Velocity,'mach')
 
ans = 
    unit of  "mach"
    3.9063    3.3034    2.7832    2.3245    1.9127

The "unit" statement reports unit names with the shortest abreviation. Unit names expressed in a convert statement are reported exactly the way they are expressed. For example

x=unit(1,'yds')
x=convert(x,'meters')
 
x = 
     0.9144 m
 
x = 
     0.9144 meters

The unit names in a "convert" statement can also be created with operators. The velocity in the example above can be expressed in kilometers per hour by

convert(Velocity,'kilometers/hour')
 
ans = 
    unit of  "kilometers/hour"
  1.0e+003 *

    4.6634    3.9436    3.3226    2.7750    2.2834

Contact us