MATLAB Quirks: cellfun() high-performance trap

cellfun() is a powerful function in MATLAB which mirrors the idea of applying a ‘map’ operation (as in functional programming) to monads (cells: a datatype that holds any arbitrary data type, including itself).

One common use is to identify which cells are empty so you can ignore them. Typically, you can do

index.emptyCells = cellfun(@isempty, C);

According to the help (H1) page, there is a limited set of strings that you can use in place of the function handle (functor) for backward compatibility:

A = CELLFUN('fun', C), where 'fun' is one of the following strings,
returns a logical or double array A the elements of which are computed 
from those of C as follows:
   'isreal'     -- true for cells containing a real array, false
   'isempty'    -- true for cells containing an empty array, false 
   'islogical'  -- true for cells containing a logical array, false 
   'length'     -- the length of the contents of each cell 
   'ndims'      -- the number of dimensions of the contents of each cell
   'prodofsize' -- the number of elements of the contents of each cell
A = CELLFUN('size', C, K) returns the size along the K-th dimension of
the contents of each cell of C.
A = CELLFUN('isclass', C, CLASSNAME) returns true for each cell of C
that contains an array of class CLASSNAME.  Unlike the ISA function, 
'isclass' of a subclass of CLASSNAME returns false.

Turns out these functions have their native implementation and runs super-fast. But I got burned once when I tried to use this on cells containing table() objects:

index.emptyCells = cellfun('isempty', cellsOfTables);

I meant to find out if I have zero-row or zero-column (empty) table objects with that. It gave me all false even when my cells have these 0-by-0 tables. What a Terrible Failure?! Turns out these ‘backward compatibility’ native implementations (I guess they already have cellfun() before having function handles) looks at the raw data stream like PODs (Plain Old Datatypes) as a C program would do.

A table() object has lots of stuff stored in it like variable (column) names, so there’s no way a program looking at an arbitrary binary stream without accounting for such data type will consider that object empty. It’s up to the overloaded isempty() or numel() of the class to tell what is empty and what’s not, but it needs to be called by the function handle to establish which method to call.

Lesson learned: don’t use those special string based functor in cellfun() unless you know for sure it’s a POD. Otherwise it will get you the wrong answer at the speed of light.

769 total views, no views today

Make your MATLAB code work everywhere

MATLAB is used in many different setups that it’s hard to expect every line of your code you write generalizes to all other versions, platform and deployments. The following code examples helps you identify what envirnoment you are in and so you can branch accordingly:

% 32 bit vs 64 bit MATLAB
% This exploits the fact that mex is on the same platform as your MATLAB
is64bit = strcmpi( mexext(), 'mexw64');

% Handling different MATLAB versions
if( verLessThan('matlab', yourVersionString) )
  error('This code requires %s or above to run!', yourVersionString);

% Headless (Parallel) workers
% This exploits the fact that JVM is not loaded for parallel workers. 
% Starting MATLAB with '/nojvm' will be considered headless
isHeadless = ~usejava('desktop');

% DCOM / ActiveX /Automation Server
isDCOM = enableservice('AutomationServer');

% Test for compiled MATLAB:

216 total views, 0 views today

Auto Mouse Mover / Clicker

At work I came across a piece of fine software (fine as the ‘F’ in RTFM) to access the data from an open-source project that decided to throw a warning dialog box every time when it come across some minor data integrity issues while going through tens of thousands of entries.

Upon inspecting the C code, they failed to provide a mechanism to suppress the warning dialog box for Windows (it can be done a function that uses with signals in linux though). Since this is a one-shot gig anyway and I don’t have the time to figure out the dependencies and recompile their code chain, I came up with something ghetto: automated mouse clicker.

It’s the kind of solution that is not too natural to come by for people with finer control over computers (aka, software engineers), but immediately obvious to middle-school level script kiddies.

I have to admit I almost lost the ‘street’ knack as it took me an hour digging through the documentation and code base to find a ‘proper’ way to do it before I even gave the ghetto patch a thought. That’s how seasoned engineers can get pwned by noobs who ‘don’t know a damn thing’ but ‘gets the job done’. Luckily, I remembered to switch hats early enough this time.

A ghetto trick or two can be used ONLY when we are really sure that it’s a one-time gig that won’t be reused. Any attempts to extend the one-time-wonder can only attribute to pain and misery for everybody, as a recurring theme in software and product development projects.

Sounds like a familiar situation at work? It’s always that one small thing your client asked you for. If you hack something up quick-‘n’-dirty and it worked, they are going to say, ‘by the way, one more one small thing…’. Lather, rinse, and repeat and soon you’re buried in brown, sticky foam.

This can be explained by the life lesson I learned in my first summer internship during high school: people in general don’t know what they really wanted and they don’t communicate well. Me included. The soft-skills coaching received at Stanford helped quite a bit, but I still consider myself ‘not there yet’.

Typically, your client is going to ask you for something specific (which is not what they really wanted because they haven’t figured it out yet) and keep revising and making sharp turns at your expense to make up for the ‘errors’ as in ‘trial-and-error’.

This is not a win-win scenario. It’s a tug of war between you and your client: you want your client to do their homework to figure out what they wanted and your client want to crap-shoot at your expense. It creates tension between you and your client. A work contract (or a specification agreed upon) might protect you from blame, but a client that doesn’t get what he/she wanted (regardless of who is at fault) is not going to be happy about your work.

One way out of it is to ask your client to start with a vague, underlying theme of what they are trying to accomplish. This way they won’t be overwhelmed by the pressure to get all the detailed specs right. If you have a long term relationship with the client, it pays to learn what are their general ‘interests’ are so you can piece the fragments together quicker. Gradually, you can ease them into thinking through their underlying goals aloud and you can take the opportunity to explore how you can help them accomplish it.

Guide them through their thought process, and maybe show a little prototype or design to help them visualize how they can accomplish their goals (through you). With the big picture in mind, you can make educated judgement, fill in details, and decide what intermediate/iterative steps are needed instead of letting the client unwittingly micromanage you based off his/her solution approach by saying ‘I need one small thing quick…’ one at a time. You are now in control over the project and in a very good position to help your client meet his/her true agenda.

It seemed like more upfront work to meet your client halfway and do part (or half) of their homework, but for anything other than a short hit-and-run, it’s less total work for you and a happier outcome for both. Using this approach, I was able to deliver a last minute change in ‘1 minute’ because the software architecture I designed was tailored towards the client’s underlying goal, so the feature he forgot to ask for was naturally built in and only requires one line of code to turn it on despite I hadn’t explicitly planned for it.

The bottom line is that humans are clueless (at various levels) and they want to further their interests (by doing something). If we take both parties’ interest into account and spot inefficiencies that we can correct, there’s at least an approach where everybody wins. Game-theoretic approach and market equilibrium is ‘optimal’ only when ‘technology’ is held unimproved and there is no on-going relationship between parties. Life doesn’t always have to be a war or struggle 🙂

199 total views, no views today

“Copy as path” option in Microsoft Windows

Very often I want to copy the full path of a file to my MATLAB environment for quick testing. Before my coworker showed me the trick, I tried to staple the filename I copied from the Explorer window to the address bar then re-copy the whole. That was dumb.

For some reason, Windows stowed away a slick feature called ‘Copy as Path’. You have to hold “SHIFT” key while right-clicking to get the good stuff displayed (same as “Open Command Window Here”).  What a crime!

This website shows you a way to have the menu option activated by default:

128 total views, no views today

Garfield and Friends’ ‘Italian’ BGM

Besides Anamaniacs, Garfield and Friends has always been my favorite TV cartoon show in the early 90s.

They both used classics for BGM like ‘Dance of the Sugar Plum Fairy’ and ‘Humoresque’ because it’s cheap to license and highly recognizable for parody purposes. For decades I’ve been wondering what is the name of the background music used when something ‘Italian’ is involved … one day I heard my boss (a Brazillian with an Italian descent) humming that and I asked him what it was. Turns out it’s called Tarantella Napoletana.

260 total views, no views today