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:
Supported units and their abbreviations:
centimeters per second squared (cmps2)
Earth gravities (G)
feet per second squared (ftps2)
meters per second squared (mps2)
millimeters per second squared (mmps2)
nanometers per second squared (nmps2)
square centimeters (cm2)
square feet (ft2)
square inches (in2)
square kilometers (km2)
square meters (m2)
square miles (mi2)
square millimeters (mm2)
square yards (yd2)
ENERGY AND WORK:
British thermal units (Btu)
electron volts (eV)
kilocalories or nutritional calories (kcal)
radians per second (radps)
revolutions per minute (rpm)
astronomical units (au)
light years (ly)
micrometers or microns (um)
nautical miles (nautmi)
atomic mass units (amu)
boiler horsepower (hpb)
British thermal units per hour (Btuph)
electrical horsepower (hpe)
mechanical horsepower (hp)
standard atmospheres (atm)
dynes per square centimeter (dynpcm2)
feet of water column at 4°C (ftH2O)
inches of water column at 4°C (inH2O)
millimeters of mercury at 0°C (mmHg)
pounds per square inch (psi)
feet per second (ftps)
kilometers per hour (kmph)
mach number at STP (mach)
miles per hour (mph)
meters per second (mps)
cubic centimeters (cc or cm3)
cubic feet (ft3)
cubic inches (in3)
cubic kilometers (km3)
cubic meters (m3)
cubic miles (mi3)
cubic yards (yd3)
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.
Chad Greene (2020). Unit Converters (https://www.mathworks.com/matlabcentral/fileexchange/35258-unit-converters), MATLAB Central File Exchange. Retrieved .
Units and unit conversions are now in MATLAB if you have Symbolic Math Toolbox. Try:
>> u = symunit;
>> x = 2*u.meter
>> x = rewrite(x,u.ft) % convert using "rewrite" function
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~
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.
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
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;
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 ;)
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.
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.
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.
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"
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.
Fixed a typo in cm32l--thanks for catching the error, Maximilian.
Now with more significant digits. Thanks for the suggestions Erich.
Edited description text.
Most powerful version to date! Now with hp, Btuph, W, kW, MW, GW, TW, hpe, and hpb!
The most powerful version to date! W, hp, hpe, hpb, kW, MW, GW, TW, Btuph.
A lot of energy went into making this update: Btu, cal, kcal, eV, erg, ftlb, Nm, J, kJ, kWh.
Turned up the volume in this release. cl, cc, ml, cm3, ft3, in3, km3, m3, mi3, gal, l, oz, qt, yd3.
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.
Area: acre, ha, cm2, ft2, in2, km2, m2, mi2, mm2, yd2.
Computing: bits and bytes.
Pressure conversions: bar, mbar, psi, hPa, Pa, kPa, MPa, ftH2O, inH2O, mmHg, Torr, atm, dynpcm2.
Force (or weight) conversions now included.
Massive addition to the bundle. Added mass conversions, mostly.
Added conversions for time, frequency, and speed. Still plenty more to come.
Now, with angstroms! Added many more length converter functions in this update. More to come soon!
Added acceleration converters, clarified documentation, added occasional commentary.
edited description text