{"id":5440,"date":"2014-07-18T09:00:08","date_gmt":"2014-07-18T13:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5440"},"modified":"2014-07-18T09:49:31","modified_gmt":"2014-07-18T13:49:31","slug":"simple-namespaces-and-brilliant-function-handles","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/07\/18\/simple-namespaces-and-brilliant-function-handles\/","title":{"rendered":"Simple Namespaces and Brilliant Function Handles"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg's<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/38014-module-encapsulate-matlab-package-into-a-name-space-module\">module - encapsulate MATLAB package into a name space module<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15478\">Daniel Dolan<\/a>.\r\n      <\/p>\r\n      <p>The concept of namespaces has been available in MATLAB for a number of years at this point. While the technology is very mature\r\n         at this point, a common feature of namespaces was not included in the MATLAB implementation. You cannot call one function\r\n         in the same namespace from another function in the same namespace without including the namespace definition somewhere. Most\r\n         namespaces in other languages permit functions to call each other within the namespace without needing to identify the namespace.\r\n      <\/p>\r\n      <p>Daniel&#8217;s &#8220;MODULE&#8221; function provides an interesting means to work around that constraint using a method that demonstrates the\r\n         powerful concept of function handles in MATLAB.\r\n      <\/p>\r\n      <p>The nature of Daniel&#8217;s solution also provides a means to create namespace specifiers, without needing to continually reference\r\n         the full namespace.\r\n      <\/p>\r\n      <p>Finally Daniel makes it possible to have what one might call &#8220;dynamic namespaces&#8221;.<\/p>\r\n      <p>I selected this submission because it demonstrates the powerful feature of function handles, advertises the use of namespaces\r\n         in MATLAB, and uses the &#8220;DBSTACK&#8221; function in a way that surprised me.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Before you use this function, be sure to fix the bug<\/a><\/li>\r\n         <li><a href=\"#2\">What is a namespace?<\/a><\/li>\r\n         <li><a href=\"#5\">Namespaces present challenges too<\/a><\/li>\r\n         <li><a href=\"#7\">So what does module do?<\/a><\/li>\r\n         <li><a href=\"#10\">What about the namespace self-referencing constraint in MATLAB?<\/a><\/li>\r\n         <li><a href=\"#12\">It&#8217;s all about the function handles<\/a><\/li>\r\n         <li><a href=\"#13\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Before you use this function, be sure to fix the bug<a name=\"1\"><\/a><\/h3>\r\n   <p>Yes, it happens. There&#8217;s what appears to be a bug in this File Exchange submission. Change line <i>110<\/i> from\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">root=sprintf(<span style=\"color: #A020F0\">'%s.'<\/span>,root{:});<\/pre><p>to<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">root=sprintf(<span style=\"color: #A020F0\">'%s.'<\/span>,root{end:-1:1});<\/pre><p>The order in which package namespaces is being constructed is backward. Changing this line reverses the construction and resolves\r\n      the issue.\r\n   <\/p>\r\n   <h3>What is a namespace?<a name=\"2\"><\/a><\/h3>\r\n   <p>If you&#8217;re not familiar with namespaces, don&#8217;t fret. It is an advanced software engineering concept. I work with a lot of software\r\n      engineers all over the world, and many of them are not familiar with namespaces because they work in coding languages or environments\r\n      that don&#8217;t have them (such as C).\r\n   <\/p>\r\n   <p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Namespace\">namespace<\/a> provides a means to specifically call unique functions that share the same name. I like to think of a namespace as a context\r\n      or a classifier for a function.\r\n   <\/p>\r\n   <p>Let&#8217;s say you have two functions, each named &#8220;myDisp&#8221;, but they have different behavior.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><pre style=\"font-style:oblique\">My Display 1\r\nHello\r\n<\/pre><p>Versus<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><pre style=\"font-style:oblique\">My Display 2\r\n-------------\r\nHello\r\n<\/pre><p>How do you invoke the appropriate <i>myDisp<\/i> at the correct time?\r\n   <\/p>\r\n   <p>Perhaps you could give the functions different names: <i>myDisp<\/i> and <i>myDisp2<\/i>.\r\n   <\/p>\r\n   <p>But if you&#8217;re concerned about modularity, and these functions are used independently in other projects and code files, changing\r\n      the name of the functions will be problematic.\r\n   <\/p>\r\n   <p>What if instead you could include an identifier that would make the call to the <i>myDisp<\/i> function more specific?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myFirstNamespace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)\r\n&gt;&gt; myOtherNamespace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><p>Now both functions can be available, without introducing a name conflict, or needing to change the name of the function.<\/p>\r\n   <p>To accomplish this in MATLAB, you need to use <a href=\"#brf3g8k\">package folders<\/a><\/p>\r\n   <h3>Namespaces present challenges too<a name=\"5\"><\/a><\/h3>\r\n   <p>While namespaces provide a means to be more specific about function calls, they can be inconvenient when it comes to actually\r\n      writing them out, and reading them in the code file. Especially as the namespaces get longer.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myMainNamespace.myOtherSubNamespace.myOtherNamespace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><p>This seems hard to read, and a pain to write.<\/p>\r\n   <p>We can use the <a href=\"\">IMPORT<\/a> function to handle this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; import myMainNamespace.mySubNamespace.myFirstNamespace.*\r\n&gt;&gt; myDisp(<span style=\"color: #A020F0\">'hello'<\/span>)<\/pre><pre style=\"font-style:oblique\">My Display 1\r\nHello\r\n<\/pre><p>Now contents of the myFirstNamespace can be referenced without using the full namespace notation. But if you import two functions\r\n      with the same name but live in different namespaces, you are back to the same problem that namespaces try to solve! You can\r\n      partially import namespaces, but this gets tedious.\r\n   <\/p>\r\n   <h3>So what does module do?<a name=\"7\"><\/a><\/h3>\r\n   <p>The MODULE function makes it possible to refer to functions in namespace by essentially providing an alias for the namespace.\r\n       This means you can refer to functions deep within a namespace without needing to write out the full namespace, or have the\r\n      potential conflict issues using the IMPORT function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myLocalSpace = module(<span style=\"color: #A020F0\">'myMainNamespace.mySubNamespace.myFirstNamespace'<\/span>);\r\n&gt;&gt; myLocalSpace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">&gt;&gt; myOtherLocalSpace = module(<span style=\"color: #A020F0\">'myMainNamespace.myOtherSubNamespace.myOtherNamespace'<\/span>);\r\n&gt;&gt; myOtherLocalSpace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><p>Here the calls to the functions in the namespace are concise and specific to the desired function.<\/p>\r\n   <p>This also allows you to do fun things like make the namespace dynamic<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> (a &lt; b)\r\n    myLocalSpace = module(<span style=\"color: #A020F0\">'myMainNamespace.mySubNamespace.myFirstNamespace'<\/span>);\r\n<span style=\"color: #0000FF\">else<\/span>\r\n    myLocalSpace = module(<span style=\"color: #A020F0\">'myMainNamespace.myOtherSubNamespace.myOtherNamespace'<\/span>);\r\n<span style=\"color: #0000FF\">end<\/span>\r\nmyLocalSpace.myDisp(<span style=\"color: #A020F0\">'Hello'<\/span>)<\/pre><p>I cannot say at this point whether or not having dynamic namespaces is a good idea, but it is certainly an interesting concept.<\/p>\r\n   <h3>What about the namespace self-referencing constraint in MATLAB?<a name=\"10\"><\/a><\/h3>\r\n   <p>In MATLAB, if a function in a namespace calls another function in the same namespace, it must refer to the full namespace\r\n      of the function being called.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/modeulWriteup\/currentFolder.png\"> <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> myDisp( displayInput )\r\n    displayHeader =\r\n    myMainNamespace.myOtherSubNamespace.myOtherNamespace.otherFunction;\r\n    disp(displayHeader);\r\n    disp(displayInput);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>This is fine, until later when you wrap the existing namespace in another parent namespace, and you must go into all of the\r\n      functions and append the new parent namespace. If you have hundreds of functions, this is going to get old fast.\r\n   <\/p>\r\n   <p>Daniel provides a really neat mechanism to get around this constraint.  Calling MODULE with no inputs assumes the namespace\r\n      you want is relative to the current function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> myDisp( displayInput )\r\n    localNamespace = module;\r\n    displayHeader = localNamespace.otherFunction();\r\n    disp(displayHeader);\r\n    disp(displayInput);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Now, even if the structure of the namespace changes, you can still call the functions local to the namespace, because the\r\n      local namespace is automatically updated when the <i>myDisp<\/i> function is called.\r\n   <\/p>\r\n   <h3>It&#8217;s all about the function handles<a name=\"12\"><\/a><\/h3>\r\n   <p>This is the part I think is really elegant.<\/p>\r\n   <p>In order to make this work, Daniel takes advantage of <a href=\"\">function handles<\/a>. Function handles are pointers or aliases to functions. This means you can use a variable to store a reference to a function.\r\n   <\/p>\r\n   <p>Daniel creates a collection of these function handles by creating function handles for each of the functions in a specific\r\n      package folder. Then he stores these function handles in a MATLAB struct. <a href=\"\">Subreferencing the struct<\/a> using the dot notation (struct.field) will now act like referencing a function in a namespace. How cool is that!\r\n   <\/p>\r\n   <p>Finally, to get around the constraint MATLAB imposes of functions in a namespace referencing other functions in the same namespace,\r\n      Daniel uses the <a href=\"\">DBSTACK<\/a> function.\r\n   <\/p>\r\n   <p>I&#8217;m pretty sure he&#8217;s using DBSTACK in a way it was never intended, but I think it&#8217;s absolutely brilliant!<\/p>\r\n   <p>Daniel uses the DBSTACK function to determine what function called MODULE, so he can then figure out what namespace the function\r\n      belongs to.\r\n   <\/p>\r\n   <p>While there may be some performance implications from using DBSTACK, it is an excellent demonstration of the flexibility of\r\n      the MATLAB environment. You can ask a function &#8220;Who called you?&#8221; I&#8217;ve been working with MATLAB over 15 years now, and I didn&#8217;t\r\n      realize you could do that!\r\n   <\/p>\r\n   <h3>Comments<a name=\"13\"><\/a><\/h3>\r\n   <p>If you would like to leave any comments regarding this post, please click <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5440#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_64b4a4168a464147a5b84e70c9b23561() {\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='64b4a4168a464147a5b84e70c9b23561 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 64b4a4168a464147a5b84e70c9b23561';\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 = 'Greg Wolff';\r\n        copyright = 'Copyright 2014 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_64b4a4168a464147a5b84e70c9b23561()\"><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; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\n64b4a4168a464147a5b84e70c9b23561 ##### SOURCE BEGIN #####\r\n%% Simple Namespaces and Brilliant Function Handles\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\r\n% Greg's> pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/38014-module-encapsulate-matlab-package-into-a-name-space-module\r\n% module - encapsulate MATLAB package into a name space module> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15478 Daniel\r\n% Dolan>.\r\n% \r\n% The concept of namespaces has been available in MATLAB for a number of\r\n% years at this point. While the technology is very mature at this point, a\r\n% common feature of namespaces was not included in the MATLAB\r\n% implementation. You cannot call one function in the same namespace from\r\n% another function in the same namespace without including the namespace\r\n% definition somewhere. Most namespaces in other languages permit functions\r\n% to call each other within the namespace without needing to identify the\r\n% namespace.\r\n% \r\n% Daniel\u00e2\u20ac\u2122s \u00e2\u20ac\u0153MODULE\u00e2\u20ac\ufffd function provides an interesting means to work around\r\n% that constraint using a method that demonstrates the powerful concept of\r\n% function handles in MATLAB.\r\n% \r\n% The nature of Daniel\u00e2\u20ac\u2122s solution also provides a means to create namespace\r\n% specifiers, without needing to continually reference the full namespace.\r\n% \r\n% Finally Daniel makes it possible to have what one might call \u00e2\u20ac\u0153dynamic\r\n% namespaces\u00e2\u20ac\ufffd.\r\n% \r\n% I selected this submission because it demonstrates the powerful feature\r\n% of function handles, advertises the use of namespaces in MATLAB, and uses\r\n% the \u00e2\u20ac\u0153DBSTACK\u00e2\u20ac\ufffd function in a way that surprised me.\r\n% \r\n%% Before you use this function, be sure to fix the bug\r\n% Yes, it happens. There\u00e2\u20ac\u2122s what appears to be a bug in this File Exchange\r\n% submission. Change line _110_ from \r\n%\r\n% \r\n%   root=sprintf('%s.',root{:});\r\n%  \r\n% to  \r\n% \r\n%   root=sprintf('%s.',root{end:-1:1});\r\n% \r\n% The order in which package namespaces is being constructed is backward. Changing this line reverses\r\n% the construction and resolves the issue.\r\n%% What is a namespace?\r\n% If you\u00e2\u20ac\u2122re not familiar with namespaces, don\u00e2\u20ac\u2122t fret. It is an advanced\r\n% software engineering concept. I work with a lot of software engineers all\r\n% over the world, and many of them are not familiar with namespaces because they work\r\n% in coding languages or environments that don\u00e2\u20ac\u2122t have them (such as C).\r\n% \r\n% A <http:\/\/en.wikipedia.org\/wiki\/Namespace namespace> provides a means to\r\n% specifically call unique functions that share the same name. I like think\r\n% of a namespace as a context or a classifier for a function.\r\n%\r\n% Let\u00e2\u20ac\u2122s say you\r\n% have two functions, each named \u00e2\u20ac\u0153_myDisp_\u00e2\u20ac\ufffd, but they have different\r\n% behavior. \r\n%\r\n%\r\n%   >> myDisp('Hello') \r\n%\r\n%\r\naddpath(fullfile(pwd, 'Test'));\r\nmyMainNamespace.mySubNamespace.myFirstNamespace.myDisp('Hello')\r\n%%\r\n% Versus \r\n%\r\n%\r\n%   >> myDisp('Hello') \r\n%\r\n%\r\nmyMainNamespace.myOtherSubNamespace.myOtherNamespace.myDisp('Hello') \r\n%%\r\n% How do you invoke the appropriate _myDisp_ at the correct time?\r\n%\r\n% Perhaps you could give the functions different names: _myDisp_ and _myDisp2_.\r\n%\r\n% But if you\u00e2\u20ac\u2122re concerned about modularity, and these functions are used\r\n% independently in other projects and code files, changing the name of the\r\n% functions will be problematic. \r\n%\r\n% What if instead you could include an\r\n% identifier that would make the call to the _myDisp_ function more specific?\r\n% \r\n% \r\n%   >> myFirstNamespace.myDisp('Hello') \r\n%   >> myOtherNamespace.myDisp('Hello') \r\n%\r\n%\r\n% Now both functions can be\r\n% available, without introducing a name conflict, or needing to change the\r\n% name of the function. \r\n%\r\n% To accomplish this in MATLAB, you need to use\r\n% <#brf3g8k package folders>\r\n% \r\n%% Namespaces present challenges too\r\n% While namespaces provide a means to be more specific about function\r\n% calls, they can be inconvenient when it comes to actually writing them\r\n% out, and reading them in the code file. Especially as the namespaces get\r\n% longer. \r\n%\r\n%\r\n%   >> myMainNamespace.myOtherSubNamespace.myOtherNamespace.myDisp('Hello')\r\n%\r\n% \r\n% This seems hard to read, and a pain to write. \r\n%\r\n% We can use the < IMPORT>\r\n% function to handle this: \r\n%\r\n%\r\n%   >> import myMainNamespace.mySubNamespace.myFirstNamespace.* \r\n%   >> myDisp('hello') \r\n%\r\n%\r\nmyMainNamespace.mySubNamespace.myFirstNamespace.myDisp('Hello')\r\n%%\r\n% Now\r\n% contents of the myFirstNamespace can be referenced without using the full\r\n% namespace notation. But if you import two functions with the same name\r\n% but live in different namespaces, you are back to the same problem that\r\n% namespaces try to solve! You can partially import namespaces, but this\r\n% gets tedious.\r\n% \r\n%% So what does module do?\r\n% The MODULE function makes it possible to refer to functions in namespace\r\n% by essentially providing an alias for the namespace.  This means you can\r\n% refer to functions deep within a namespace without needing to write out\r\n% the full namespace, or have the potential conflict issues using the\r\n% IMPORT function.\r\n% \r\n%\r\n%   >> myLocalSpace = module('myMainNamespace.mySubNamespace.myFirstNamespace'); \r\n%   >> myLocalSpace.myDisp('Hello') \r\n%\r\n%\r\n\r\n%%\r\n%\r\n%\r\n%   >> myOtherLocalSpace = module('myMainNamespace.myOtherSubNamespace.myOtherNamespace');\r\n%   >> myOtherLocalSpace.myDisp('Hello')\r\n%\r\n%\r\n\r\n%%\r\n% Here the calls to the functions in the\r\n% namespace are concise and specific to the desired function. \r\n%\r\n% This also\r\n% allows you to do fun things like make the namespace dynamic\r\n%\r\n%\r\n%   if (a < b)\r\n%       myLocalSpace = module('myMainNamespace.mySubNamespace.myFirstNamespace');\r\n%   else \r\n%       myLocalSpace = module('myMainNamespace.myOtherSubNamespace.myOtherNamespace'); \r\n%   end\r\n%   myLocalSpace.myDisp('Hello') \r\n%\r\n%\r\n% I cannot say at this point whether or not\r\n% having dynamic namespaces is a good idea, but it is certainly an\r\n% interesting concept.\r\n%% What about the namespace self-referencing constraint in MATLAB?\r\n% In MATLAB, if a function in a namespace calls another function in the\r\n% same namespace, it must refer to the full namespace of the function being\r\n% called. \r\n%\r\n% <<currentFolder.png>> \r\n%\r\n%\r\n%   function myDisp( displayInput )\r\n%       displayHeader =\r\n%       myMainNamespace.myOtherSubNamespace.myOtherNamespace.otherFunction;\r\n%       disp(displayHeader);\r\n%       disp(displayInput);\r\n%   end\r\n%\r\n%\r\n% This is\r\n% fine, until later when you wrap the existing namespace in another parent\r\n% namespace, and you must go into all of the functions and append the new\r\n% parent namespace. If you have hundreds of functions, this is going to get\r\n% old fast. \r\n%\r\n% Daniel provides a really neat mechanism to get around this\r\n% constraint.  Calling MODULE with no inputs assumes the namespace you want\r\n% is relative to the current function. \r\n%\r\n%\r\n%   function myDisp( displayInput )\r\n%       localNamespace = module;\r\n%       displayHeader = localNamespace.otherFunction();\r\n%       disp(displayHeader);\r\n%       disp(displayInput);\r\n%   end\r\n%\r\n%\r\n\r\n%%\r\n% Now, even if the structure of the namespace changes, you can still call\r\n% the functions local to the namespace, because the local namespace is\r\n% automatically updated when the _myDisp_ function is called. \r\n%\r\n%% It\u00e2\u20ac\u2122s all about the function handles \r\n% This is the part I think is really elegant. \r\n%\r\n% In order to make this work, Daniel takes advantage of\r\n% <\r\n% function handles>. Function handles are pointers or aliases to functions.\r\n% This means you can use a\r\n% variable to store a reference to a function.\r\n% \r\n% Daniel creates a collection of these function handles by creating\r\n% function handles for each of the functions in a specific package folder.\r\n% Then he stores these function handles in a MATLAB struct. < Subreferencing\r\n% the struct> using the dot notation (struct.field) will now act like\r\n% referencing a function in a namespace. How cool is that!\r\n% \r\n% Finally, to get around the constraint MATLAB imposes of functions in a\r\n% namespace referencing other functions in the same namespace, Daniel uses\r\n% the < DBSTACK> function. \r\n%\r\n% I\u00e2\u20ac\u2122m pretty sure he\u00e2\u20ac\u2122s using DBSTACK in a way it was\r\n% never intended, but I think it\u00e2\u20ac\u2122s absolutely brilliant! \r\n%\r\n% Daniel uses the\r\n% DBSTACK function to determine what function called MODULE, so he can then\r\n% figure out what namespace the function belongs to. \r\n%\r\n% While there may be\r\n% some performance implications from using DBSTACK, it is an excellent\r\n% demonstration of the flexibility of the MATLAB environment. You can ask a\r\n% function \u00e2\u20ac\u0153Who called you?\u00e2\u20ac\ufffd I\u00e2\u20ac\u2122ve been working with MATLAB over 15 years\r\n% now, and I didn\u00e2\u20ac\u2122t realize you could do that!\r\n% \r\n% \r\n%% Comments\r\n%\r\n% If you would like to leave any comments regarding this post, please click\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5440#respond here>.\r\n##### SOURCE END ##### 64b4a4168a464147a5b84e70c9b23561\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/modeulWriteup\/currentFolder.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Greg's pick this week is module - encapsulate MATLAB package into a name space module by Daniel Dolan.\r\n      \r\n      The concept of namespaces has been available in MATLAB for a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/07\/18\/simple-namespaces-and-brilliant-function-handles\/\">read more >><\/a><\/p>","protected":false},"author":36,"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\/5440"}],"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\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5440"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5440\/revisions"}],"predecessor-version":[{"id":5446,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5440\/revisions\/5446"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}