File Exchange

image thumbnail

Unit Converters

version (577 KB) by Chad Greene
Convert units of pressure, length, time, force, mass, accel., temp, speed, frequency, area, & more!


Updated 06 Aug 2014

View Version History

View License

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:


ans =


Supported units and their abbreviations:

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)

degrees (deg)
radians (rad)

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)

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

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)

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

gigahertz (GHz)
hertz (Hz)
kilohertz (kHz)
megahertz (MHz)
radians per second (radps)
revolutions per minute (rpm)
terahertz (THz)

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)

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

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)

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)

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

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

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

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.

Cite As

Chad Greene (2021). Unit Converters (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Karan Gill

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

>> u = symunit;
>> x = 2*u.meter
x =

>> x = rewrite(x,u.ft) % convert using "rewrite" function
x =

>> double(separateUnits(x))
ans =

See the tutorial:

Chen Qi

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~


nedal marei



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.

Chad Greene

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.


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.

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



Chad Greene


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.


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

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.

Chad Greene

Hi Jean-Yves,

You may be looking for something along these lines: 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

Hi Chad,
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.


Works as advertised.

Chad Greene

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!


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"

Chad Greene

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.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!