{"id":3754,"date":"2012-07-27T07:34:35","date_gmt":"2012-07-27T12:34:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3754"},"modified":"2012-07-27T07:35:39","modified_gmt":"2012-07-27T12:35:39","slug":"1000-unit-converters-at-your-fingertips","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/07\/27\/1000-unit-converters-at-your-fingertips\/","title":{"rendered":"1000 unit converters at your fingertips!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters\">Unit Converters<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/225623\">Chad Greene<\/a>.\r\n   <\/p>\r\n   <p>This week's pick comes from <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/07\/13\/you-make-the-call-take-2\/#comment-14634\">Sam G's recommendation<\/a>. I really like what he has to say about it:\r\n   <\/p>\r\n   <p><i>I like functions that I can use without asking myself, \"now how do I use this function again? What are the inputs and outputs?\r\n         What is the name of the function?\" The NaN suite and unit conversion functions both meet this criteria.<\/i><\/p>\r\n   <p>He makes a great point. I always make sure (and encourage others) to comment the code and create very meaningful help. But\r\n      having a well thought-out function name and calling syntax can make a world of difference. I also make sure that the variable\r\n      name used for the input arguments are well-named. This ensures that the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_env\/brrx4m4-1.html#brrx5j1-1\">function hints<\/a> display a meaningful text.\r\n   <\/p>\r\n   <p>So what do Chad's functions do? In a nutshell, they are a collection of over a 1000 functions for converting units of various\r\n      quantities, including acceleration, angle, area, computing, force, frequency, energy, length, mass, power, pressure, speed,\r\n      temperature, time, and volume. His functions follow a consistent naming convention of \"from2to\". For example, 3 miles in kilometers\r\n      is,\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mi2km(3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n    4.8280\r\n<\/pre><p><p>In the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters#feedback\">feedback section<\/a> of the entry, I noticed a few dialogs about whether the functions should be organized in separate folders. I've chimed in\r\n      suggesting the use of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_oop\/brfynt_-1.html\">packages<\/a>. Ultimately, it also has to do with preference, and Chad makes a good point that his functions are very easy to remember\r\n      and use, so simply putting everything in one folder and adding it to the path is sufficient.\r\n   <\/p>\r\n   <p>But since I brought it up, let me explain a little bit about the benefits of packages in this case. Packages allow you to\r\n      put functions together in their own namespace. In Chad's case, you can put the 1000+ functions into a folder, and name the\r\n      folder starting with a \"+\". For example, I put them in a folder named \"+convert\". Then, as long as the folder containing \"+convert\"\r\n      is in the MATLAB path, you can call them like this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">convert.mi2km(3)<\/pre><p><p>By using packages, your functions need only be uniquely named within the package. You won't have to worry about other functions\r\n      developed by others clashing with your function names.\r\n   <\/p>\r\n   <p>Another benefit of packages is the ease of discoverability of the functions. Some of you may have used <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_env\/brrx4m4-1.html#bswj2of-1\">tab-completion<\/a> to find functions you're looking for. By putting functions in packages, you can narrow the scope of the search to just the\r\n      package. If I type \" <tt>convert.m2<\/tt> \" and the Tab key, it will show only the functions that start with \" <tt>m2<\/tt> \" in my \"convert\" package.\r\n   <\/p>\r\n   <p>Typically, if I'm creating individual functions that aren't related to each other, I don't bother placing them in packages.\r\n      But when I have lots of related functions, that's when I may use packages.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Thanks Chad for giving us over a thousand useful conversion functions at our fingertips. And thanks Sam G for the recommendation!<\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3754#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters#comments\">comment<\/a> for Chad.\r\n   <\/p>\r\n   <p>By the way, a few months ago <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a> wrote a Pick of the Week <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/02\/03\/units-conversion-toolbox\/\">post<\/a> on another <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox\">unit conversion tool<\/a> . It's a very powerful class with utilities for automatic conversion and error checking. I highly recommend checking it out\r\n      as well.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d784823c82a8415ab0e53a4aba34bfdb() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='d784823c82a8415ab0e53a4aba34bfdb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d784823c82a8415ab0e53a4aba34bfdb';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2012 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_d784823c82a8415ab0e53a4aba34bfdb()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p>\r\n<\/div>\r\n<!--\r\nd784823c82a8415ab0e53a4aba34bfdb ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters Unit\r\n% Converters> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/225623\r\n% Chad Greene>.\r\n%\r\n% This week's pick comes from\r\n% <https:\/\/blogs.mathworks.com\/pick\/2012\/07\/13\/you-make-the-call-take-2\/#comment-14634\r\n% Sam G's recommendation>. I really like what he has to say about it:\r\n%\r\n% _I like functions that I can use without asking myself, \"now how do I use\r\n% this function again? What are the inputs and outputs? What is the name of\r\n% the function?\" The NaN suite and unit conversion functions both meet this\r\n% criteria._\r\n%\r\n% He makes a great point. I always make sure (and encourage others) to\r\n% comment the code and create very meaningful help. But having a well\r\n% thought-out function name and calling syntax can make a world of\r\n% difference. I also make sure that the variable name used for the input\r\n% arguments are well-named. This ensures that the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_env\/brrx4m4-1.html#brrx5j1-1\r\n% function hints> display a meaningful text.\r\n%\r\n% So what do Chad's functions do? In a nutshell, they are a collection of\r\n% over a 1000 functions for converting units of various quantities, including\r\n% acceleration, angle, area, computing, force, frequency, energy, length,\r\n% mass, power, pressure, speed, temperature, time, and volume. His\r\n% functions follow a consistent naming convention of \"from2to\". For\r\n% example, 3 miles in kilometers is,\r\n\r\nmi2km(3)\r\n\r\n%%\r\n% In the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters#feedback\r\n% feedback section> of the entry, I noticed a few dialogs about whether the\r\n% functions should be organized in separate folders. I've chimed in\r\n% suggesting the use of\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_oop\/brfynt_-1.html\r\n% packages>. Ultimately, it also has to do with preference, and Chad makes\r\n% a good point that his functions are very easy to remember and use, so\r\n% simply putting everything in one folder and adding it to the path is\r\n% sufficient.\r\n%\r\n% But since I brought it up, let me explain a little bit about the benefits\r\n% of packages in this case. Packages allow you to put functions together in\r\n% their own namespace. In Chad's case, you can put the 1000+ functions into\r\n% a folder, and name the folder starting with a \"+\". For example, I put\r\n% them in a folder named \"+convert\". Then, as long as the folder containing\r\n% \"+convert\" is in the MATLAB path, you can call them like this:\r\n%\r\n%   convert.mi2km(3)\r\n% \r\n% By using packages, your functions need only be uniquely named within the\r\n% package. You won't have to worry about other functions developed\r\n% by others clashing with your function names.\r\n%\r\n% Another benefit of packages is the ease of discoverability of the\r\n% functions. Some of you may have used\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/matlab_env\/brrx4m4-1.html#bswj2of-1\r\n% tab-completion> to find functions you're looking for. By putting\r\n% functions in packages, you can narrow the scope of the search to just the\r\n% package. If I type \" |convert.m2| \" and the Tab key, it will show only\r\n% the functions that start with \" |m2| \" in my \"convert\" package.\r\n%\r\n% Typically, if I'm creating individual functions that aren't related to\r\n% each other, I don't bother placing them in packages. But when I have lots\r\n% of related functions, that's when I may use packages.\r\n%\r\n% *Comments*\r\n%\r\n% Thanks Chad for giving us over a thousand useful conversion functions at\r\n% our fingertips. And thanks Sam G for the recommendation!\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=3754#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35258-unit-converters#comments\r\n% comment> for Chad.\r\n%\r\n% By the way, a few months ago\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>\r\n% wrote a Pick of the Week\r\n% <https:\/\/blogs.mathworks.com\/pick\/2012\/02\/03\/units-conversion-toolbox\/\r\n% post> on another\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox unit\r\n% conversion tool> . It's a very powerful class with utilities for\r\n% automatic conversion and error checking. I highly recommend checking it\r\n% out as well.\r\n\r\n##### SOURCE END ##### d784823c82a8415ab0e53a4aba34bfdb\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is Unit Converters by Chad Greene.\r\n   \r\n   This week's pick comes from Sam G's recommendation. I really like what he has to say about it:\r\n   \r\n   I like functions that I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/07\/27\/1000-unit-converters-at-your-fingertips\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3754"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3754"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3754\/revisions"}],"predecessor-version":[{"id":3760,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3754\/revisions\/3760"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}