Thursday, September 10, 2009

Deleting legend entries in Matlab plots

Matlab has been a constant source of annoyance and rage since I started writing my thesis. Using it in a dual screen environment, trying to export figures, and the source file editor's obscure keyboard short-cuts often brought me close to insanity.

But today I want to share a nice trick with you that I have just found out: How to delete legend entries in a Matlab plot.

Why would anyone want to do that? An example is the errorbarbar script. It adds errorbars to your bar plot. A pretty useful thing! What is not so useful is the fact that the errorbars themselves appear in the legend. See for yourself:

>> values=10*rand(5, 3);
>> errors=rand(5,3);
>> errorbarbar(values, errors);

The resulting image, after a legend has been inserted using Insert -> Legend:


The data4, ..., data 5 entries are both useless and annoying.

Here's the solution. In the figure window, click Edit -> Properties. Then, select an error bar and enter

>> hasbehavior(gco, 'legend', false);

Do this for each bar in the first group. Then delete the old legend and insert a new one using Insert -> Legend. VoilĂ :


Looks much better, doesn't it?