@Davide: How would I monitor the heap memory? Is there a way to see it in Activity Monitor? I've set it to the maximum (256 MB) in the MATLAB Preferences but am still running into the Java Heap Memory errors when I'm repeatedly executing long code that generates lots of images (the problem does not occur in the first run but only after 3-4 runs). jheapcl is not preventing the error from happening, even if I execute it in the preamble.
Thanks for sharing this excellent code. I just tried to write data and then add column headers but found that xlwrite (as opposed to xlswrite) does not append to an already existing file but rather replaces it. Am I missing something?