### Using Handle Graphics to Change an Existing Plot

Let’s say you have an existing plot, saved as a .fig file, and you’d like to change the width of one of the plot lines. How do you do it? Do you have to generate the plot again? Nope - you can use MATLAB’s handle graphics to update the existing figure. For more info on how to do this, see this excellent page at MIT:

http://www.mit.edu/p … e/matlab/handle.html

If, for example, you want to change the ordering (layering) of two separate plot lines, you can do something similar to:

>> h = gca;
>> get(h, 'children')
>> set(h, 'children', [ans(2), ans(1)]);


Very handy!

### MATLAB Has a Map Type…

In one of my recent coding projects, I needed a map data structure, i.e., something that would store key/value pairs - similar to a dictionary in Python. I’d been planning to write something up myself (hacked together), but a little bit of searching turned up:

m = containers.Map;


which was just what I needed. This is a hashmap implementation, which was (apparently) introduced in R2008b. Adding new key/value pairs is straightforward:

m('Jim') = 'Brother';


for example. There’s lots more - check it out by typing

help containers.Map

at the MATLAB command prompt.

### Syntactic Sugar: Using the New ‘~’ Notation

Here’s the situation: you call a function that returns multiple outputs, and you only need to use the second or third output - you’d like to ignore the rest. In one case I encounter often:


[dummy, idx] = max(x);
...


I’m interested in the index of the maximum value in the array ‘x’, but I don’t need to know the value itself. So I create a dummy variable, which hangs around unused, cluttering things up. Starting with MATLAB R2009b, you can now do the following instead:


[~, idx] = max(x);


This is the new ‘~’ (tilde) notation, which explicitly indicates (to both the programmer and m-lint) that the first output argument is unused. It’s a little bit of syntactic sugar that makes the code cleaner, and avoids the need for an extra variable. Sweet!

### LaTeX Output from MATLAB Symbolic Expressions

I’ve recently been doing a lot of work with the symbolic toolbox - some of the matrices I produce are large, and I need to get the results into LaTeX. I’d previously done this manually or with a quick shell script - but it turns out that MATLAB already has the command latex, which takes a symbolic expression and generates LaTeX output. Even better, if you name your symbolic variables with underscores, the command will generate proper subscripts! For example:

>> syms q_1 q_2 q_3 real
>> q = [q_1; q_2; q_3]
q =
q_1
q_2
q_3
>> latex(q)
ans =
\left(\begin{array}{c} q_{1}\\ q_{2}\\ q_{3} \end{array}\right)


Very handy!