*Fun fact - "ginormous" is apparently a valid word in Google's spell-check
In keeping with the theme of the first post of this series, these are all stupid little things that are not particularly artful or earth-shattering, but they are dang useful tool/concepts that really helped me to be a lot more productive once I understood them. So here's hoping this also helps you!
To read Part 1 of this series in which I covered the joys of Google and debugger, go here. To read the currently non-existent next post, don't go here.
And now.....
Functions and Scripts and Cell Scripts, oh my!
You were expecting the Wizard of Oz, weren't you? Life is full of dissapointments, isn't it? On the other hand, you may enjoy reading about the Venezuelan Trench Moose. |
Functions:
Stuff in, stuff out. Basically a make-your-own version of Matlab's functions (think "plot","mean", etc.). You feed in variables, crunching happens, and it plots what you tell it to plot and spits out what you tell it to spit out*. The thing I like about functions is that they don't spit out all the intermediary variables into your nice clean workspace. So if you've got some calculation that requires 17 steps, you only have to deal with step 1 and step 17. It's also possible to stick your custom functions inside other functions, thus giving you 9 or 17 or 200 less lines of code to sift through.
*And therein lies the danger of coding, because computers are like extremely literal-minded 4-year-olds with access to ALL your data. They follow instructions VERY well.
Of course the catch is that if something goes wrong in the middle, you don't see it. Which is why we need debugger! And because it can sometimes be obnoxious to have to run functions over and over and over again to get them right, I'll sometimes work out algorithms first in a....
Script:
Stuff in, stuff happens and all of it (variables, plots, etc.) gets kept. Basically it's a faster way of executing a whole bunch of commands in the workspace (the place next to the >> symbol where you generally enter single commands). For this reason I like using scripts to record how I made figures*, especially if they're finicky one of a kind figures. I can keep track of what data I used and exactly how I plotted it, so if I lose the image file or need to alter something, I don't have to start from scratch.
*Grad students! THIS IS IMPORTANT!!! LEARN FROM MY MISTAKES!!!
In the same vein, scripts are incredibly useful for batch-importing data, or basically any task where it's a pain to do the steps one at a time in the command line, but you're likely not going to do it again so it's not worth the effort to create a slick function.
Run the whole script by either pressing the green "play" button at the top, or by typing the name of the script file in the command line and pressing Enter.
Cell Scripts:
Like a script but with structure. You can split a script into cells by putting "%%" in a line between cell blocks. You can then run the script a cell at a time by placing your cursor inside the cell and hitting Enter to run the cell or hitting ctrl+Enter to run the cell and then advance to the next one. Cell scripts are mostly useful for keeping an unruly script organized, or for situations where you're going to want to execute part of a script several times (say for adjusting parameters on a figure). HOWEVER, you can't use debugger in cell mode (at least I think you can't), which is annoying.
Command history
Another sanity saving tool. Too lazy to keep hitting up in the command line to get that previous command back? Just go find it in command history, and double click to execute or copy-paste it in the command line if you want to alter something.
I also find this highly useful for building scripts and functions, and just keeping track of what I've been doing. When I'm processing data at work, I'll typically keep a chart going with fields for variable, the exact command I used to create that variable, and comments on the outcome*.
*i.e. Wow. According to this gravity meter the world clearly just exploded.
If you're not being good and keeping track in a handy chart, you can also word-search the command line to try to find just what exactly you did to create that graph two months ago! Unfortunately, it only goes back so far, so better to keep a chart/script going...
Save
Save the workspace! SAVE THE WORKSPACE!!* Preferably BEFORE you accidentally close Matlab after wrangling that data set for three hours. Instructions here. This is incredibly useful if you ever have to go back and look at old data, or heaven forbid reprocess it.
*Bonus points if you figured out this is what I was referencing. I hope from now on ALL of you read anything beginning with "save the" in Julia Child's voice.
Next up: Plot editor, the File Exchange, and Concatenation