Non-Obvious MATLAB TricksJuly 16, 2013
Most of these examples also apply to GNU Octave, a highly-compatible free software alternative to MATLAB.
This is a compilation of MATLAB tricks I've picked up over the past few years. If you'd like to add something to this page, send me an email or a Pull request on Github. Your work will be attributed.
Print Elapsed Time
When solving a problem in MATLAB, you'll often have a choice between two or three valid methods, some more efficient than others. How to evaluate them? One way is to check execution time using the tic and toc commands. Adding tic right before the function/loop/whatever you want to evaluate in your script starts a timer that runs until the toc command appears, at which point the elapsed time is printed to the command window.
Solve Systems of Equations with Backslash
Say you have a system of equations Ax = b. If A is invertible (e.g., det(A) is non-zero), then you can solve for x using x = A\b. This is pretty much the same as using x = inv(A)*b, but smarter and faster about it because it examines the matrix first to determine the easiest way to perform the inversion. More info can be found by calling help mldivide.
Text Formatting With Special Characters
There are a variety of alignment options that you can use to make strings and command window output easier to read. While you generate these by typing "\n" (for example) in the code, what you're actually doing is printing invisible Unicode control characters that are processed by the text viewer.
- \n You probably know this one. It starts a new line.
- \r Carriage return. This goes back to the beginning of the line. You basically only need it if you are printing to an actual text file that you are going to open with Notepad on Windows, because Windows by default ignores mere "newline" commands and requires \r\n instead. You're fine on Mac and UNIX-like systems, though.
- \t Insert a horizontal tab character. Sometimes this is easier than using printf-style formatting to achieve your desired layout (i.e., generating a simple table, or tab-delimited CSV output).
More info about the more exotic special characters can be found on the formatting strings page from the Mathworks.
LaTeX in Figure Windows
MATLAB already looks for and processes LaTeX commands in graph titles and axes labels. For example, if you want the x-axis label to be \(\sigma_x\), you can use xlabel '\sigma_x'. Superscripts are denoted by ^, subscripts by _. Be careful, though—if you want an entire word in the subscript, like \(\alpha_{principal}\), you have to enclose it in curly braces: \alpha_{principal}, or you'll end up with \(\alpha_principal\) (only the first letter subscripted).
Anonymous Functions
Anonymous functions are little one-line functions that you can define
on-the-fly. Unlike regular functions, they can be defined in scripts,
not just function-files. Here's an example of something you could type
on the command line:
>>myfun = @(x,y) x+y;
>>myfun(2,3)
ans =
5
This creates a function of x and y called
myfun that returns x+y. So myfun(2,3) returns 5
as shown.
The Mathworks help page for anonymous functions is pretty good in this case. Note: Matlab's definition of 'anonymous' is a little loose since the function handle is assigned to a variable. Other languages do things differently (see lambda abstraction on Wikipedia) .
Avoid the Power Function (x^y) if Speed is a Priority
Here's an example from Chris Peratrovich that describes the issue: Caret Speed Test.
By the way, the document itself is a good example of Matlab's HTML Export capability.