{"id":9898,"date":"2018-06-22T10:43:41","date_gmt":"2018-06-22T14:43:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9898"},"modified":"2018-06-22T10:43:41","modified_gmt":"2018-06-22T14:43:41","slug":"function-handle-constructor","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/06\/22\/function-handle-constructor\/","title":{"rendered":"Function Handle Constructor"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45941\">function_handle constructor<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1711536\">Rody Oldenhuis<\/a>.<\/p><p>When I first started programming in MATLAB, I would create my MATLAB files (scripts, functions) in my current folder. Everything is there, so I had no issues calling my scripts and functions. As I started working on multiple projects, I would create a new working folder and put everything there. After a while, I would have these similar functions that I constantly reuse. That was around the time I learned about the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/what-is-the-matlab-search-path.html\">path<\/a>. I can put all of my utility functions in a particular folder and add that folder to my search path. Later on, I learned about other ways of organizing my code, like <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/private-functions.html\">private functions<\/a>. When I learned about object oriented programming, I discovered the existence of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/scoping-classes-with-packages.html\">packages<\/a>.<\/p><p>Once in a while, I have these situations where I need to call a function that lives in some other folder, but that folder is not on the path. I just need it for that particular task, and I don&#8217;t want to add it to the path just for that task. There is a handy function called <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/run.html\"><tt>run<\/tt><\/a>, which allows you to call any <em>script<\/em> that exist elsewhere, but it doesn&#8217;t work for functions. This is where <tt>function_handle<\/tt> by Rody comes into play.<\/p><p>For example, there was a blog post I did a few years ago where I highlighted an entry that converted numbers to English names. The function isn&#8217;t on my path, but let me show you how it works. First, I&#8217;ll create a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/creating-a-function-handle.html\">function handle<\/a> for that function. Normally, you just use the @ symbol to create a function handle. But for a function that is not on the path, it becomes a bit tricky. Here, I&#8217;ll use Rody&#8217;s <tt>function_handle<\/tt>.<\/p><pre class=\"codeinput\">fcn = function_handle(<span class=\"string\">'..\\2015_01_09_Num2Words\\num2words\\num2words.m'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">fcn =\r\n  function_handle with value:\r\n    @num2words\r\n<\/pre><p>Now I can just call <tt>num2words<\/tt> using the function handle.<\/p><pre class=\"codeinput\">fcn(1024)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n    'one thousand and twenty-four'\r\n<\/pre><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=9898#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45941#comment\">comment<\/a> for Rody.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_38253a05369f4307902c8e92c39af32d() {\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='38253a05369f4307902c8e92c39af32d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 38253a05369f4307902c8e92c39af32d';\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        copyright = 'Copyright 2018 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_38253a05369f4307902c8e92c39af32d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2018a<br><\/p><\/div><!--\r\n38253a05369f4307902c8e92c39af32d ##### SOURCE BEGIN #####\r\n%%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45941\r\n% function_handle constructor> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1711536 Rody\r\n% Oldenhuis>.\r\n%\r\n% When I first started programming in MATLAB, I would create my MATLAB\r\n% files (scripts, functions) in my current folder. Everything is there, so\r\n% I had no issues calling my scripts and functions. As I started working on\r\n% multiple projects, I would create a new working folder and put everything\r\n% there. After a while, I would have these similar functions that I\r\n% constantly reuse. That was around the time I learned about the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/what-is-the-matlab-search-path.html\r\n% path>. I can put all of my utility functions in a particular folder and\r\n% add that folder to my search path. Later on, I learned about other ways\r\n% of organizing my code, like\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/private-functions.html\r\n% private functions>. When I learned about object oriented programming, I\r\n% discovered the existence of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/scoping-classes-with-packages.html\r\n% packages>.\r\n% \r\n% Once in a while, I have these situations where I need to call a function\r\n% that lives in some other folder, but that folder is not on the path. I\r\n% just need it for that particular task, and I don't want to add it to the\r\n% path just for that task. There is a handy function called\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/run.html |run|>, which allows\r\n% you to call any _script_ that exist elsewhere, but it doesn't work for\r\n% functions. This is where |function_handle| by Rody comes into play.\r\n%\r\n% For example, there was a blog post I did a few years ago where I\r\n% highlighted an entry that converted numbers to English names. The\r\n% function isn't on my path, but let me show you how it works. First, I'll\r\n% create a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/creating-a-function-handle.html\r\n% function handle> for that function. Normally, you just use the @ symbol\r\n% to create a function handle. But for a function that is not on the path,\r\n% it becomes a bit tricky. Here, I'll use Rody's |function_handle|.\r\n\r\nfcn = function_handle('..\\2015_01_09_Num2Words\\num2words\\num2words.m')\r\n\r\n%%\r\n% Now I can just call |num2words| using the function handle.\r\n\r\nfcn(1024)\r\n\r\n%%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=9898#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45941#comment\r\n% comment> for Rody.\r\n\r\n##### SOURCE END ##### 38253a05369f4307902c8e92c39af32d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\nJiro&#8216;s pick this week is function_handle constructor by Rody Oldenhuis.When I first started programming in MATLAB, I would create my MATLAB files (scripts, functions) in my current folder&#8230;. <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/06\/22\/function-handle-constructor\/\">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\/9898"}],"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=9898"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9898\/revisions"}],"predecessor-version":[{"id":9902,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9898\/revisions\/9902"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}