View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Edit linestyles of plot legends

5.0 | 7 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.7 KB File ID: #22116 Version: 1.1

Edit linestyles of plot legends



15 Nov 2008 (Updated )

Simple fix for adding markers to plot legend lines.

| Watch this File

File Information

If a plot is sampled very densely, and one wants to add markers to the plot, this can be done by plotting two lines on top of each other. One being the smooth line, the other being a down-sampled version with the markers only (e.g. diamonds, squares)

Now, the above is fine as long as we don't need a plot legend, since the legend function will scan for all lines, and what we have is a combination.

This simple script is a work-around to the problem. There is also an example file included showing how to use the function.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
13 Aug 2012 Pearl

Pearl (view profile)

just what I needed! Thank you.

13 Dec 2011 Steven

Steven (view profile)

It was easy enough to add this myself, but it'd be nice to let the user specify the linewidth. I used this on a plot with several different line widths making the results unpredictable with the current program. Sometimes I would like to have all the same linewidths in the legend.

Otherwise, this was exactly what I've been looking for. Thanks!

Comment only
13 Dec 2011 Steven

Steven (view profile)

27 Oct 2011 Johan Carlson

It appears MATLAB does not save exactly what has been drawn, but some command that should recreate it, in this case the legend command. So, re-running the legend command will of course destroy the changes made by legendlinestyles.m

A workaround would be to save the code you used to create the plot, rather than the fig itself. This is what I always do, which is probably why I never ran into this problem.

So, my conclusion is that this is a problem with how MATLAB saves figures, not with legendlinestyles.m as such. Will play around with some other legend changes, though, and see where it takes me.

Comment only
27 Oct 2011 Johan Carlson

If I print (or export) figures to, for example, .eps, everything works. If I save to a .fig file, I get the same error. Very strange, indeed, and I'm not even sure where to start looking. It's like MATLAB arbitrarily discards of some of the manually entered annotations.

Anyway, will look into it. Please keep me posted if you find a solutions.


Comment only
27 Oct 2011 Thomas Drozdek

thanks! that's exactly what i need!
just one problem...if i save the figure and open it again, the legend is without the marker :(
can someone help me?

10 Nov 2010 Brock

Brock (view profile)

Very nice script, does exactly what you say it does. Much easier than editing all this stuff manually. Thanks!

05 Jun 2009 Goutham Mylavarapu

Thanks. I had this problem and your code worked for me. Another code 'hack legend' also works the same way

12 May 2009 Johan Carlson

I now posted an updated version, incorporating the changes suggested by Martin. It should be available for download any day now.

Comment only
11 May 2009 Johan Carlson

Good suggestion. will do!

Comment only
09 May 2009 Martin Huber

very good! please also add color change:

%define colors
colors={'k',[0 0 1]};

%in your function:
if ischar(colors{m})==1

02 Mar 2009 Lorgio Teodovich

excellent, thank.

12 May 2009 1.1

The function has been updated to also permit changing the color of lines and markers in the legend. The function is still backwards-compatible (so code using version 0.5 should still work)

Contact us