MATLAB Techniques: Who’s your daddy? Ask dbstack(). Unusual uses of dbstack()

Normally having your function know about its caller (other than through the arguments we pass onto the stack) is usually a very bad idea in program organization, because it introduces unnecessary coupling and hinders visibility.

Nonetheless, debugger is a built-in feature of MATLAB and it provides dbstack() so you have access to your call stack as part of your program. Normally, I couldn’t come up with legitimate uses of it outside debugging.

One day, I was trying to write a wrapper function that does the idiom (mentioned in my earlier blog post)

fileparts( mfilename('fullpath') );

because I want the code to be self-documenting. Let’s call the function mfilepath(). Turns out it’s a difficult problem because mfilename(‘fullpath’) reports the path of the current function executing it. In the case of a wrapper, it’s the path of the wrapper function, not its caller that you are hoping to extract its path from.

In other words, if you write a wrapper function, it’s the second layer of the stack that you are interested in. So it can be done with dbstack():

function p = mfullpath()
  ST = dbstack('completenames');
  try
    p = ST(2).file;
  catch
    p = '';
  end

Since exception handling is tightly knit into MATLAB (unlike C++, which you pay for the extra overhead), there aren’t much performance penalty to use a try…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 there is only one way for this ST(2).file access operation to fail: mfullpath() is called from the base workspace.

Speaking of catchy titles, I wonder why Loren Shure, a self-proclaimed lover of puns and the blogger of the Art of MATLAB, haven’t exploited the built-in functions ‘who’ and ‘whos’ in her April Fools jokes like

whos your daddy
who let the dogs out

Note that these are legitimate MATLAB syntax that won’t throw you an exception. Unless you have ‘your’, ‘daddy’, ‘let’, ‘the’, ‘dogs’, ‘out’ as variable names, the above will quietly show nothing. It’d be hilarious if they pass that as an easter egg in the official MATLAB. They already have ‘why’,

why not

Error using rng (line 125)
First input must be a nonnegative integer seed less than 2^32, 'shuffle', 'default', or generator settings captured previously using S = RNG.

Error in why (line 10)
 dflt = rng(n,'v5uniform');

 

 

 

Loading

Ebay customer support’s on-hold music: Weird Al?!!

A couple of months ago, before this blog started, I called eBay’s customer support and I was put on hold for a whole hour. It would have made my day miserable, but the on-hold music they played made my day: it’s Weird Al Yankovic’s parody song eBay!

Couldn’t stop chuckling for the first half hour despite the gruesome wait … I just couldn’t get enough of it even if loops every minute or two! I kept thinking: this can’t be true!

Yes, the eBay’s official hotline plays Weird Al’s parody of eBay to their customers while they wait! They didn’t even bleep out the word “crap” as in “this crap, shows up in, bubble wrap, almost every day“. Well, I guess they have to, as “crap” is essential to the spirit of the entire parody!

I love these people. They really have a sense of humor, and they are willing to make fun of themselves!

Loading

Title of this blog site

Initially I started with “TMI: Too Much Information” as the title of this blog, given that my plan was to put fragments of technical information or insights I came across that might be useful for solving problems. That means the blog posts contain more than what you want to know, unless you are looking to solve a specific problem with the help of the post or you are just outright nerdy.

But soon I realized I have some non-technical stuff like gags, music, and the technical stuff covers more than just electronic measurement instruments, so I need a title that’s less common and more catchy.

Today, I came across a reddit post, which user “llllIlllIllIlIRogue Sysadmin “says:

If you don’t they’ll just hear jargon and glaze over completely and not even try to follow you. If you draw a pretty layout of everything, though, they’ll make some token effort to follow along.

They’ll still get lost but now you’re not just a nerd rambling… you’re a rambling nerd with a plan.

Very catchy! Also, I liked the original comment because it covers:

  • Passion for geeky topics
  • It’s important to communicate well so that people will bother to follow what we have to say.

I did a google search with quotes “rambling nerd with a plan” and only one entry: the original post, showed up, so it’s not a commonly used phrase. I’ll take it 🙂

Loading

MATLAB Quirks: struct with no fields are not empty

As far as struct() is concerned, I’m more inclined to using Struct of Array (SoA) over Array of Structs (AoS), unless all the use cases screams for SoA. Performance and memory overhead are the obvious reasons, but the true motivation for me to use SoA is that I’m thinking in terms of table-oriented programming (which I’ll discuss in later posts. See table() objects.): each field of a struct is a column in a table (heterogeneous array).

Since a table() is considered empty (by isempty()) if it has EITHER 0 rows INCLUSIVE OR 0 columns (no fields) and the default constructor creates a 0 \times 0 table, I thought struct() would do the same. NOT TRUE!

First of all, the default constructor of struct() gives ONE struct with NO FIELDS (so it’s supposed to correspond to a 1 \times 0 table). What’s even harder to remember is that struct2table(struct()) gives a 0 \times 0 table.

The second thing I missed is that a struct() with NO fields is NOT empty. You can have 3 structs with NO fields! So isempty(struct()) is always false!

I usually run into this problem when I want to seed the execution with an empty struct() and have the loop expand the fields if the file has contents in it, and I’ll check if the seeded struct was untouched to see if I can read data from the file. Next time I will remember to call struct([]) instead of struct(). What a trap!

At the end of the day, while struct is powerful, but I rarely find AoS necessary to do what I wanted once table() is out. AoS has pretty much the same restrictions as in table() that you cannot put different types in the same field across the AoS, but table allows you to index with variables (struct’s field) or rows (struct array index) without changing the data structure (AoS <-> SoA). So unless it’s a performance critical piece of the code, I’ll stick with tables() for most of my struct() needs.

 

Loading

MATLAB Techniques: onCleanup() ‘destructor’

If your program opens a file, creates a timer(), or whatever resources that needs to be closed when they are no longer needed, before R2008a, you have to put your close resource calls at two places: one at the end of successful execution, the other at the exception handling in try…catch block:

FID = fopen('a.txt')
try
   // ... do something here
   fclose(FID);
catch
   fclose(FID);
end

Not only it’s messy that you have to duplicate your code, it’s also error prone when you add code in between. If your true intention is to close the resource whenever you exit the current program scope, there’s a better way for you: onCleanup() object. The code above can be simplified as:

FID = fopen('a.txt')
obj = onCleanup(@() fclose(FID));
// ... do something with FID here

The way onCleanup() works is that it creates a dummy object which you register a function handle that does the cleanup through the constructor argument, and the destructor will blindly call what you’ve registered (the cleanup functor) when its lifecycle ends.

Due to copyright reasons, I won’t copy the simple code here. Just open onCleanup.m in MATLAB editor and you’ll see it that the code (excluding comments) has less words than the description above. Pretty neat!

Normally we use onCleanup() inside a function. The best place to put is is right after you opened a resource because anything in between can go wrong (i.e. might throw exceptions): you want ‘obj’ to be swept (i.e. its destructors called) when that happens.

Technically, you can make an onCleanup() object in the base (root) workspace (aka command window). The destructor will be triggered either when you clear the obj explicitly using clear or when you exit MATLAB. You can see for yourself with this:

obj = onCleanup(@() pause);

It kind of let you do a one-off cleanup on exit instead of a recurring cleanup in finish.m.

So the next time you open a resource that needs to be closed whether the program exits unexpectedly or not, use onCleanup()! It’s one of the elegant, smart uses of OOP.


Connection to C++ concept: RAII

This MATLAB technique is an extension of C++’s RAII where you manage the resource exclusively through acquiring resources at constructors and do cleanup at destructors and let the object’s lifecycle managing the timing.

The innovation in onCleanup() is that it saves simple usages (like opening a file or device) from defining a custom class just to take advantage of the destructor:

  • The cleanup class itself is generic and its definition/implementation do not tie to (in other words decoupled from) your application. Your application-specific code is what you’ve registered to an instance of this dummy onCleanup class through the constructor.
  • It’s one line to construct the dummy object with simple cleanup code tucked under an anonymous function (lambda).
  • You can take advantage of a function scope to implicitly delete the cleanup object (thus calling the cleanup operations) or explicitly delete the cleanup object with clear.

GCC and Clang has a __cleanup__ compiler extension (non-standard) attribute that attaches the cleanup function pointer to the variable (resource) to be called when the variable’s lifetime expires. Note that C does not have destructors and the cleanup function is directly attached to the said variable, not a dummy variable that shares the same lifecycle as the resource.

Loading