讀中學時喺Newsgroup成日睇都有人用cap圖回覆好頂癮,諗住要貯低啲圖留番嚟寸人。而家晤使咁麻煩啦。有嗰網頁叫 WhatsCap 有齊晒啲材料。
xlsread() and everything that calls it, such as readtable(), is terribly slow, especially when you have a boatload of Excel files to process. The reason behind it is that xlsread() closes that DCOM handle (which closes the Excel COM session) after it finishes, and restart Excel (DCOM) again when you call xlsread() again to load another spreadsheet file.
That means there’s a lot of opening and closing of the Excel application if you process multiple spreadsheets. The better strategy, which is covered extensively in MATLAB’s File Exchange (FEX), is to have your program open one Excel handle, and process all the spreadsheets within the same DCOM handle before closing it.
This strategy is quite overwhelming for a beginner, and even if you use FEX entries, you still cannot get around the fact that you have to know there’s a handle that manages the Excel session and remember to close it after you are done with it. Nothing beats having xlsread() do it automatically for you.
Starting with R2015b, the Excel DCOM handle called by xlsread() is now persistent: that after you make the first call to xlsread(), Excel.exe will stay in the memory unless you explicitly clear persistent variables or exit MATLAB, so you can reuse them every time xlsread() or xlswrite() is called. Finally!
The code itself is pretty slick. You can find it in ‘matlab.io.internal.getExcelInstance()’. Well, I guess it’s not hard to come up with it, but I guess in TMW, they must have a heated debate about whether it’s a good idea to keep Excel around (taking up resources) when you are done with it. With the computation power required to run R2015b, an extra Excel.exe lying around should be insignificant. Good call!
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');
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!