{"id":264,"date":"2016-08-15T21:37:08","date_gmt":"2016-08-16T05:37:08","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=264"},"modified":"2016-11-27T13:04:32","modified_gmt":"2016-11-27T21:04:32","slug":"matlab-techniques-whos-your-daddy-ask-dbstack","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/08\/15\/matlab-techniques-whos-your-daddy-ask-dbstack\/","title":{"rendered":"MATLAB Techniques: Who&#8217;s your daddy? Ask dbstack()."},"content":{"rendered":"<p>Normally having your\u00a0function know about its caller (other than through the arguments we pass onto the stack) is usually a very bad\u00a0idea in program organization, because it introduces unnecessary coupling and hinders visibility.<\/p>\n<p>Nonetheless, debugger is a built-in\u00a0feature of MATLAB and it provides dbstack() so you have access to your call stack as part of your program. Normally, I couldn&#8217;t come up with legitimate uses of it outside debugging.<\/p>\n<p>One day, I was trying to\u00a0write a wrapper function that does the idiom (mentioned in my earlier blog <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/07\/23\/structuring-your-matlab-code-base\/\">post<\/a>)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">fileparts( mfilename('fullpath') );<\/pre>\n<p>because I want the code to be self-documenting. Let&#8217;s call the function mfilepath(). Turns out it&#8217;s a difficult problem because mfilename(&#8216;fullpath&#8217;) reports the path of the current function executing it. In the case of a wrapper, it&#8217;s the path of the wrapper function, not its caller that you are hoping to extract its path from.<\/p>\n<p>In other words, if you write a wrapper function, it&#8217;s\u00a0the\u00a0second layer of the stack that you are interested in. So it can be done with dbstack():<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">function p = mfullpath()\r\n  ST = dbstack('completenames');\r\n  try\r\n    p = ST(2).file;\r\n  catch\r\n    p = '';\r\n  end<\/pre>\n<p>Since exception handling is tightly knit into MATLAB (unlike C++, which you pay for the extra overhead), there aren&#8217;t much performance penalty to use a try&#8230;catch() block than if I checked if ST actually have a second layer (i.e. has a non-base caller). I can confidently do that because\u00a0there is only one way for this ST(2).file access operation to fail:\u00a0mfullpath() is called from the base workspace.<\/p>\n<p>Speaking of catchy titles, I wonder why Loren Shure, a self-proclaimed lover of <a href=\"http:\/\/blogs.mathworks.com\/loren\/2015\/11\/13\/an-old-adage-in-matlab\/?s_tid=srchtitle\">puns<\/a>\u00a0and the blogger of the <a href=\"http:\/\/blogs.mathworks.com\/loren\">Art of MATLAB,<\/a>\u00a0haven&#8217;t exploited the built-in functions &#8216;who&#8217; and &#8216;whos&#8217; in her April Fools jokes like<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">whos your daddy\r\nwho let the dogs out\r\n<\/pre>\n<p>Note that these are legitimate MATLAB syntax that won&#8217;t throw you an exception. Unless you have &#8216;your&#8217;, &#8216;daddy&#8217;, &#8216;let&#8217;, &#8216;the&#8217;, &#8216;dogs&#8217;, &#8216;out&#8217; as variable names, the above will\u00a0quietly show nothing. It&#8217;d be hilarious if they pass that as an easter egg in the official MATLAB. They already have &#8216;why&#8217;,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">why not\r\n\r\nError using rng (line 125)\r\nFirst input must be a nonnegative integer seed less than 2^32, 'shuffle', 'default', or generator settings captured previously using S = RNG.\r\n\r\nError in why (line 10)\r\n dflt = rng(n,'v5uniform');<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_264\" class=\"pvc_stats all  \" data-element-id=\"264\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Normally having your\u00a0function know about its caller (other than through the arguments we pass onto the stack) is usually a very bad\u00a0idea in program organization, because it introduces unnecessary coupling and hinders visibility. Nonetheless, debugger is a built-in\u00a0feature of MATLAB &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/08\/15\/matlab-techniques-whos-your-daddy-ask-dbstack\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_264\" class=\"pvc_stats all  \" data-element-id=\"264\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=264"}],"version-history":[{"count":4,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":4087,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/264\/revisions\/4087"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}