# 1000 unit converters at your fingertips!3

Posted by Jiro Doke,

Jiro's pick this week is Unit Converters by Chad Greene.

This week's pick comes from Sam G's recommendation. I really like what he has to say about it:

I like functions that I can use without asking myself, "now how do I use this function again? What are the inputs and outputs? What is the name of the function?" The NaN suite and unit conversion functions both meet this criteria.

He makes a great point. I always make sure (and encourage others) to comment the code and create very meaningful help. But having a well thought-out function name and calling syntax can make a world of difference. I also make sure that the variable name used for the input arguments are well-named. This ensures that the function hints display a meaningful text.

So what do Chad's functions do? In a nutshell, they are a collection of over a 1000 functions for converting units of various quantities, including acceleration, angle, area, computing, force, frequency, energy, length, mass, power, pressure, speed, temperature, time, and volume. His functions follow a consistent naming convention of "from2to". For example, 3 miles in kilometers is,

mi2km(3)
ans =
4.8280


In the feedback section of the entry, I noticed a few dialogs about whether the functions should be organized in separate folders. I've chimed in suggesting the use of packages. Ultimately, it also has to do with preference, and Chad makes a good point that his functions are very easy to remember and use, so simply putting everything in one folder and adding it to the path is sufficient.

But since I brought it up, let me explain a little bit about the benefits of packages in this case. Packages allow you to put functions together in their own namespace. In Chad's case, you can put the 1000+ functions into a folder, and name the folder starting with a "+". For example, I put them in a folder named "+convert". Then, as long as the folder containing "+convert" is in the MATLAB path, you can call them like this:

convert.mi2km(3)

By using packages, your functions need only be uniquely named within the package. You won't have to worry about other functions developed by others clashing with your function names.

Another benefit of packages is the ease of discoverability of the functions. Some of you may have used tab-completion to find functions you're looking for. By putting functions in packages, you can narrow the scope of the search to just the package. If I type " convert.m2 " and the Tab key, it will show only the functions that start with " m2 " in my "convert" package.

Typically, if I'm creating individual functions that aren't related to each other, I don't bother placing them in packages. But when I have lots of related functions, that's when I may use packages.

Thanks Chad for giving us over a thousand useful conversion functions at our fingertips. And thanks Sam G for the recommendation!

By the way, a few months ago Brett wrote a Pick of the Week post on another unit conversion tool . It's a very powerful class with utilities for automatic conversion and error checking. I highly recommend checking it out as well.

Get the MATLAB code

Published with MATLAB® 7.14