File Exchange

Unit Converters

version 1.24 (577 KB) by

Convert units of pressure, length, time, force, mass, accel., temp, speed, frequency, area, & more!

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Intuitive functions for conversion of acceleration, angle, area, computing, force, frequency, energy, length, mass, power, pressure, speed, temperature, time, or volume. All converters in this bundle take the form from2to().
EXAMPLE: Convert 700 kilopascals to pounds per square inch:

kPa2psi(700)

ans =

101.5264

Supported units and their abbreviations:

ACCELERATION:
centimeters per second squared (cmps2)
Earth gravities (G)
feet per second squared (ftps2)
galileos (Gal)
microgalileos (uGal)
milligalileos (mGal)
meters per second squared (mps2)
millimeters per second squared (mmps2)
nanometers per second squared (nmps2)

ANGLE:
degrees (deg)

AREA:
acres (acre)
hectares (ha)
square centimeters (cm2)
square feet (ft2)
square inches (in2)
square kilometers (km2)
square meters (m2)
square miles (mi2)
square millimeters (mm2)
square yards (yd2)

COMPUTING:
bits (bit)
bytes (B)
exabytes (EB)
gigabytes (GB)
kilobytes (KB)
megabytes (MB)
petabytes (PB)
terabytes (TB)

ENERGY AND WORK:
British thermal units (Btu)
calories (cal)
electron volts (eV)
ergs (erg)
foot-pounds (ftlb)
joules (J)
kilocalories or nutritional calories (kcal)
kilojoules (kJ)
kilowatt-hours (kWh)
newton-meters (Nm)

FORCE:
dynes (dyn)
kilopounds (kip)
kilonewtons (kN)
pounds (lbf)
newtons (N)
ounces-force (ozf)

FREQUENCY:
gigahertz (GHz)
hertz (Hz)
kilohertz (kHz)
megahertz (MHz)
revolutions per minute (rpm)
terahertz (THz)

LENGTH:
angstroms (A)
astronomical units (au)
centimeters (cm)
decimeters (dm)
feet (ft)
inches (in)
kilometers (km)
light years (ly)
meters (m)
micrometers or microns (um)
miles (mi)
millimeters (mm)
nautical miles (nautmi)
nanometers (nm)
picometers (pm)
yards (yd)

MASS:
atomic mass units (amu)
grams (g)
kilograms (kg)
pounds-mass (lbm)
microgram (ug)
milligrams (mg)
slugs (slug)

POWER:
boiler horsepower (hpb)
British thermal units per hour (Btuph)
electrical horsepower (hpe)
gigawatts (GW)
kilowatts (kW)
mechanical horsepower (hp)
megawatts (MW)
terawatts (TW)
watts (W)

PRESSURE:
standard atmospheres (atm)
bar (bar)
dynes per square centimeter (dynpcm2)
feet of water column at 4°C (ftH2O)
hectopascals (hPa)
inches of water column at 4°C (inH2O)
megapascals (MPa)
kilopascals (kPa)
millibar (mbar)
millimeters of mercury at 0°C (mmHg)
pascals (Pa)
pounds per square inch (psi)
torr (Torr)

SPEED:
feet per second (ftps)
kilometers per hour (kmph)
knots (knot)
mach number at STP (mach)
miles per hour (mph)
meters per second (mps)

TEMPERATURE:
celsius (C)
fahrenheit (F)
kelvin (K)
rankine (R)

TIME:
days (day)
hours (hr)
microseconds (us)
minutes (min)
milliseconds (ms)
nanoseconds (ns)
seconds (s)
years (yr)

VOLUME:
centiliters (cl)
cubic centimeters (cc or cm3)
cubic feet (ft3)
cubic inches (in3)
cubic kilometers (km3)
cubic meters (m3)
cubic miles (mi3)
cubic yards (yd3)
liters (l)
milliliters (ml)
US liquid gallons (gal)
US liquid ounces (oz)
US liquid quarts (qt)

Convert units, organize your code, and a year from now when you look over your old scripts, instantly recognize what units are associated with variables, before and after conversion. Other converters are available (acknowledged below), but the functions in this bundle were designed to be simpler, more comprehensive, and more intuitive. Please alert me if you find any errors.

Karan Gill

Karan Gill (view profile)

Units and unit conversions are now in MATLAB if you have Symbolic Math Toolbox. Try:

>> u = symunit;
>> x = 2*u.meter
x =
2*[m]

>> x = rewrite(x,u.ft) % convert using "rewrite" function
x =
(2500/381)*[ft]

>> double(separateUnits(x))
ans =
6.5617

Chen Qi

Chen Qi (view profile)

sweet~ if you can make a GUI interface/or app that would be so perfect. Then a hands on unit converter will be always available whenever I open matlab~

Maximilian

nedal marei

Jiro Doke

Jiro Doke (view profile)

Yes, I see your point about simplicity. For your functions, you use very meaningful names, and they are unlikely to clash with other functions in the future, so it's probably not an issue. Packages will be nice if there may be function name clashes. If someone else happens to create a function called "m2mm" (just an example), then it may shadow your function or vice versa. By putting yours in a package, it would be safe because people would always call with your package name and then the function name.

The other benefit I see with packages is the ease of discoverability of your functions. If I type "convert." and TAB, it will show all of your conversion functions. Otherwise, all of your functions are at the same level as all other MATLAB functions, so tab completion may be not as helpful. For example, if I type "m2" and then TAB, I see a bunch of yours along with a couple of MATLAB functions.

But again, you chose very explicit function names, so I think people can just start typing the conversion and will be able to find your functions.

Jiro: Thanks for the suggestion. Packaging these functions is certainly an option for those who wish to pursue it, but what would be gained by doubling the length of the function names? There's a wonderful simplicity to function names like km2mi, num2str, mean, plot, etc., as opposed to convert.km2mi, convert.num2str, compute.mean, or create.plot. I am always looking for ways to make improvements, but in my view, the simplest solution is to tuck these functions away in a folder of their own, add a path to that folder in your startup.m file, and never have to look at them again.

Jiro Doke

Jiro Doke (view profile)

Regarding the comments on putting functions into folders or creating a single "convert" function, you can put them in a package which is essentially putting them in a folder that starts with a "+". This creates a nice clean namespace for your functions.

http://www.mathworks.com/help/techdoc/matlab_oop/brfynt_-1.html

You can create a folder "+convert", and put all the functions inside there. Then make sure that the folder containing "+convert" is on the path.

When you call, you call it using

convert.km2mi(3)

Erich

Erich (view profile)

Erich,

You have a keen eye! For my work, 0.000004% error is acceptable, but I realize that's not the case for everyone. I fixed the conversions between inches and multiples of meters, as you suggested. If any others catch your eye let me know. Thanks for the feedback. Feel free to email me directly, it's chad at chadagreene dot com.

Erich

Erich (view profile)

I suggest splitting the functions into separate folders, that way users can include in their path only the functions that interest them.

I realize that using floating point numbers is an approximation, but that doesn't mean using a toolbox like this shouldn't give you an answer within a few eps. For example:
cm = in*2.54;
Good! Exact definition used. But
m = in/39.37008;
??? Why not
m = in * 0.0254;
?
In fact,
m2in(in2m(1)) gives 0.999999968000065
For any given conversion, an answer of 1 +/- a couple eps should be allowed, but this error is much greater than that.

Also, I found the function ms22Gal which behaves identically to mps22Gal. I believe ms22Gal should be removed.

P.S. I drive a Jetta TDI but so far I've only used regular diesel ;)

Jean-Yves Tinevez

Jean-Yves Tinevez (view profile)

Hi Ched,

I am myself following Edward Tufte recommendations very closely, but his advices solely concern the visual display of information, not programming.
I can however get your point.

Hi Jean-Yves,

You may be looking for something along these lines: www.mathworks.com/matlabcentral/fileexchange/29621. I wanted to write functions that are very clear, and your suggested form "convert(L2, 'miles', 'mm')" is not clear to me. I would have a hard time remembering whether L2 is being converted to miles from mm or from miles to mm. I'm a big fan of Edward Tufte, who says that good design shouldn't require mnemonics. mm2mi(L2) or mi2mm(L2) are immediately understandable to me.

If clutter in your home folder is a problem, consider putting these files in a folder of their own. Add the path to that folder in your startup.m file as described above, and you'll never have to see these 1100+ functions again.

Jean-Yves Tinevez

Jean-Yves Tinevez (view profile)

Have you considered merging ALL the numerous functions in one, that you could call with two flags, one for the source unit, one for the target units. Like for instance
>> L1 = convert(L2, 'miles', 'mm');

Because honestly right now, the 200 functions are relatively inelegant and generate a lot of clutter in the MATLAB path.

Sam

Sam (view profile)

Jan, you're right! Previously, I thought I had to use 6.68*1e-22. Then a few days ago I realized 6.68e-22 also works and it's much prettier. Makes sense that it's more computationally efficient, too. Consider my ways changed. Thanks for the tip!

Jan Simon

Jan Simon (view profile)

The explicit multiplication with the exponent in e.g. "A*6.684587122671*1e-22" is less efficient than including the exponent directly: "A*6.684587122671e-22"

To help reduce clutter, I placed these functions in a folder of their own called 'functions'. Then I created a startup.m file in my home folder, with the line:

This tells Matlab to look for the functions in that folder.

 6 Aug 2014 1.24 Fixed a typo in cm32l--thanks for catching the error, Maximilian. 3 May 2012 1.23 Now with more significant digits. Thanks for the suggestions Erich. 30 Apr 2012 1.22 Edited description text. 16 Apr 2012 1.2 Most powerful version to date! Now with hp, Btuph, W, kW, MW, GW, TW, hpe, and hpb! 16 Apr 2012 1.19 The most powerful version to date! W, hp, hpe, hpb, kW, MW, GW, TW, Btuph. 12 Apr 2012 1.14 A lot of energy went into making this update: Btu, cal, kcal, eV, erg, ftlb, Nm, J, kJ, kWh. 11 Apr 2012 1.13 Turned up the volume in this release. cl, cc, ml, cm3, ft3, in3, km3, m3, mi3, gal, l, oz, qt, yd3. 4 Apr 2012 1.12 Acceleration and gravity: Gal, mGal, uGal, nmps2, mps2, cmps2, G, ftps2, mmps2. Note that in this release, nms2 has become nmps2 for consistency with naming convention. 4 Apr 2012 1.11 Area: acre, ha, cm2, ft2, in2, km2, m2, mi2, mm2, yd2. 3 Apr 2012 1.1 Computing: bits and bytes. 2 Apr 2012 1.8 Pressure conversions: bar, mbar, psi, hPa, Pa, kPa, MPa, ftH2O, inH2O, mmHg, Torr, atm, dynpcm2. 30 Mar 2012 1.7 Force (or weight) conversions now included. 30 Mar 2012 1.6 Massive addition to the bundle. Added mass conversions, mostly. 29 Mar 2012 1.5 Added conversions for time, frequency, and speed. Still plenty more to come. 29 Mar 2012 1.4 Now, with angstroms! Added many more length converter functions in this update. More to come soon! 28 Mar 2012 1.3 Added acceleration converters, clarified documentation, added occasional commentary. 13 Mar 2012 1.2 edited description text
MATLAB Release
MATLAB 7.10 (R2010a)