{"id":3190,"date":"2012-02-03T09:51:14","date_gmt":"2012-02-03T14:51:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3190"},"modified":"2012-02-03T09:51:14","modified_gmt":"2012-02-03T14:51:14","slug":"units-conversion-toolbox","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/02\/03\/units-conversion-toolbox\/","title":{"rendered":"Units Conversion Toolbox"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox\">Units Conversion Toolbox,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/47554\">John McDermid<\/a>.\r\n   <\/p>\r\n   <p>Once, a long time ago, I was a real engineer, working on real engineering problems. My trusty HP 48S Scientific Calculator\r\n      was always by my side when I was working, largely because of its capacity to manage my units. That is, I could readily add\r\n      joules to BTUs without worrying about first converting to like units. (This occasionally annoyed my professors when I was\r\n      a student; \"my calculator did it for me was rarely well received.\" On the other hand, it gave me a [perhaps false] sense of\r\n      security when my answer to an involved question came out in appropriate fundamental units.)\r\n      <p>Time marches inevitably onward; I don't do a lot of hard engineering any more. Instead, I get to spend my days helping people\r\n      solve problems in MATLAB. Occasionally, I still need to do some unit conversion and, until recently, those occasions typically\r\n      have had me dusting off the old HP (which is still on my desk!). But I'm always pleased when I find new ways to stay in the\r\n      MATLAB environment, and John's Unit Conversion Toolbox essentially consigns my calculator to the dustbin.\r\n   <\/p>\r\n   <p>John has done a significant amount of work defining virtually every unit I can think of. It's quite easy to use, and includes\r\n      impressive documentation--including a useful \"Getting Started\" doc. It also manages prefixes (like 'femto' and 'kilo') seamlessly.\r\n      Now when I add joules to BTUs--and express the answer in either, or in fundamental units, or some different unit of energy--I\r\n      can do it right from MATLAB!\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = unit(1,<span style=\"color: #A020F0\">'milliBTU'<\/span>);\r\ny = unit(2,<span style=\"color: #A020F0\">'joule'<\/span>);\r\nz = x + y\r\nz2 = convert(z,<span style=\"color: #A020F0\">'cal'<\/span>)\r\nz3 = convert(z,<span style=\"color: #A020F0\">'dyne'<\/span>)<\/pre><pre style=\"font-style:oblique\"> \r\nz = \r\n     3.05506 m^2*kg\/s^2\r\n \r\nz2 = \r\n     0.729688 cal\r\n<\/pre><pre style=\"font-style:oblique\">Error using unit\/convert (line 315)\r\nIncompatible conversion! Fundamental units of m^2*kg\/s^2 vs. m*kg\/s^2\r\nError in unitconversion_potw (line 46)\r\nz3 = convert(z,'dyne')<\/pre><p>\r\n<\/p>\r\nJohn implemented this impressive suite using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_oop\/ug_intropage.html\">MATLAB classes<\/a>, MATLAB classes, and he took pains to ensure that arithmetic operations work as expected. And I'm encouraged that when I\r\n      tried to do something foolish there, the unit class told me about it!\r\n   <\/p>\r\n   <p>Very useful, John--thanks for sharing this!<\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3190#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for John <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_2530829bc5f649f4bbeb5a2b0c2d022a() {\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='2530829bc5f649f4bbeb5a2b0c2d022a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2530829bc5f649f4bbeb5a2b0c2d022a';\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 = 'Brett Shoelson';\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_2530829bc5f649f4bbeb5a2b0c2d022a()\"><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\n2530829bc5f649f4bbeb5a2b0c2d022a ##### SOURCE BEGIN #####\r\n%% Unit Conversion Tool\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox Units Conversion Toolbox,> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/47554 John McDermid>.\r\n\r\n%%\r\n% Once, a long time ago, I was a real engineer, working on\r\n% real engineering problems. My trusty HP 48S\r\n% Scientific Calculator was always by my side when I was\r\n% working, largely because of its capacity to manage my\r\n% units. That is, I could readily add joules to BTUs\r\n% without worrying about first converting to like units.\r\n% (This occasionally annoyed my professors when I was a\r\n% student; \"my calculator did it for me was rarely well\r\n% received.\" On the other hand, it gave me a [perhaps false]\r\n% sense of security when my answer to an involved question\r\n% came out in appropriate fundamental units.)\r\n\r\n%%\r\n% Time marches inevitably onward; I don't do a lot of hard engineering\r\n% any more. Instead, I get to spend my days helping people solve problems\r\n% in MATLAB. Occasionally, I still need to do some unit\r\n% conversion and, until recently, those occasions typically\r\n% have had me dusting off the old HP (which is still on my\r\n% desk!). But I'm always pleased when I find new ways to\r\n% stay in the MATLAB environment, and John's Unit\r\n% Conversion Toolbox essentially consigns my calculator to\r\n% the dustbin.\r\n\r\n%%\r\n% John has done a significant amount of work defining\r\n% virtually every unit I can think of. It's quite easy to\r\n% use, and includes impressive documentationREPLACE_WITH_DASH_DASHincluding a useful \"Getting\r\n% Started\" doc. It also manages prefixes (like 'femto' and\r\n% 'kilo') seamlessly. Now when I add joules to BTUsREPLACE_WITH_DASH_DASHand \r\n% express the answer in either, or in fundamental units, or\r\n% some different unit of energyREPLACE_WITH_DASH_DASHI can do it right from\r\n% MATLAB!\r\n\r\n%%\r\nx = unit(1,'milliBTU');\r\ny = unit(2,'joule');\r\nz = x + y\r\nz2 = convert(z,'cal')\r\nz3 = convert(z,'dyne')\r\n\r\n%%\r\n% \r\n% John implemented this impressive suite using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_oop\/ug_intropage.html MATLAB classes>,\r\n% MATLAB classes, and he took pains to ensure that\r\n% arithmetic operations work as expected. And I'm encouraged\r\n% that when I tried to do something foolish there, the unit\r\n% class told me about it!  \r\n\r\n%%\r\n% Very useful, JohnREPLACE_WITH_DASH_DASHthanks for sharing this!\r\n\r\n%% \r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=3190#respond comments to this blog post> are welcome. Or leave a\r\n% comment for John\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29621-units-conversion-toolbox#comments here>.\r\n\r\n##### SOURCE END ##### 2530829bc5f649f4bbeb5a2b0c2d022a\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is Units Conversion Toolbox, by John McDermid.\r\n   \r\n   Once, a long time ago, I was a real engineer, working on real engineering problems. My trusty HP 48S... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/02\/03\/units-conversion-toolbox\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/3190"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3190"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3190\/revisions"}],"predecessor-version":[{"id":3210,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3190\/revisions\/3210"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}