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?
Thanks! Just what I was looking for.
ReplyDeleteAnother way:
ReplyDeletep(1) = plot(rand(20,1),'r')
hold on
p(2) = plot(rand(20,1),'g')
p(3) = plot(rand(20,1),'b')
% add only desired legend entries
legend([p(1) p(3)],'data1','data3')
thank you, thank you, thank you.
ReplyDeleteThanks a lot...
ReplyDeletethanks a load.. use it all the time..
ReplyDeleteThanks a lot..!! saved me some time
ReplyDeleteyou can change the keyboard shortcuts for the editor from emacs style to windows style. Use file->preferences->keyboard->shortcuts->windows default set
ReplyDeleteI really like this site, it's so important to know more about this topic, keep it up and of course every time I have time I'll love to check out again
ReplyDeleteThanks a lot. I was struggling with this issue for a long time.
ReplyDeleteSB
Thanks a lot for the Anonymous at May 27, 2010 1:54 PM
ReplyDeleteIt's easy and works fine!
THANK YOU!
ReplyDeleteThank you! Just what I was looking for.
ReplyDeleteThank you!!!! You just saved me many hours of work!
ReplyDeleteThis didn't work on my MATLAB R2011a,,, what should I do!! HELP!!
ReplyDeleteThank you !!!
ReplyDeleteThanks, very useful!
ReplyDeleteWorked a treat, thank you
ReplyDeleteDanke_Thank you !!!
ReplyDeletecouldn't find this answer anywhere else. thank you!
ReplyDelete"Insightful" is the perfect word to describe this wonderful writing of yours. The artistic blend of this subject with your tone of writing made this a great read. Much love 😘.
ReplyDeleteHow to bottom