Sunday, July 13, 2014

Marvelous Matlab Must-haves: Part 1

I do believe at some point I may have promised a post on the joys of Matlab.  Well, that long awaited promise shall now come to fruition.  I have assembled, in no particular order or completeness, a jumble of some features and functions of Matlab that have proved of immense use to me in avoiding baldness when using the glorious program.

Computer programming is perhaps best done by those with fast-growing thick hair.


I initially planned to make this all one blog post, and then realized a) it was going to be a really long post that might never get published and b) if I split it up I could make it look like I was all prolific and stuff.  SO. Part 1 of the series!

If you have used Matlab extensively, many of these will no doubt be old hat to you, but if you're just learning and are fearing for the fate of your remaining cranial covering,  I hope this post (and those to come) will be of some aid to your beleaguered follicles!

Google

Perhaps a bit of an obvious one, but I include this because, just in case you needed to hear it, yes, it is okay to search the interwebs for ideas/help, and no, it does not mean you suck at programming and will never get it.  Google searching "Matlab how to do xyz" is a surprisingly excellent way to figure stuff out.  About 9 times out of 10, someone has already asked the same question, someone's written a function on File Exchange (more on that later), or there's a built-in function in Matlab, because Matlab is awesome.  The other 10% of the time you can usually get a partial answer, or at least something that will get you on the right track to the correct question.

Debugger

Learn how to use Matlab's debugger!  Seriously!  You can use the debugger in both functions and scripts (but not cell scripts).  (I'll be explaining the differences between functions, scripts, and cell scripts in a later post)

The most basic way to use debugger is to tell Matlab to enter debug mode when it hits an error.  This is instead of Matlab hitting an error and making that annoying "ting!" sound and throwing up red script with some highly technical error message that tells you nothing because you've been booted out of the function that has the variable that caused the error, but now you can't LOOK at that error because you've been booted out of the function.

But if you stop in debugger, all is well!  The variable is there, in all its empty array glory.  *sigh*.

You can get to this most useful of features by going to the "debug" menu in the editor window (the window where you write functions and scripts), and selecting "stop if error".

If your code isn't dying because of errors but is throwing up completely nonsensical data, you can also insert what are called "breakpoints" so that the function/script will stop in debugger at that point so that you can look at all the variables and make sure they're what you thought they were!*  To insert a breakpoint, click on the little line next to the line of code where you want the function/script to stop, and a little red circle will appear.  The function/script will execute everything up until that line, and then stop.  At this point you can discover that your squid variable is 10 times larger than it needs to be, turning your moose variable into the dreaded "NaN".  Or some such.

*Spolier alert - they're NEVER what you thought they were.  Ever.

You can also make a conditional breakpoint by right clicking the line and selecting "Set/Modify Condition" (you'll get a yellow circle).  This is often useful for loops, where for instance you know everything is just dandy up until i = 10 (out of 23523430 iterations) at which point all hell breaks loose and demons infest your variables and you can't remember how to properly call the Matlab "exorcise" function*.  In that case you'd set your conditional breakpoint to stop on that line when i==10, and figure out just what the heck is going on!

*Because I haven't written it yet.

Next up: Functions, Cell Scripts, and Scripts, oh my! 

(....sorry.....)

Stay tuned for more to come, at no particular scheduled time in the hopefully not-too-distant future!

No comments:

Post a Comment

Be nice, and no spam. If at all possible, please try not to destroy my faith in humanity.