File Exchange

image thumbnail

Edit linestyles of plot legends

version 1.1 (1.7 KB) by

Simple fix for adding markers to plot legend lines.



View License

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.

Comments and Ratings (12)


Pearl (view profile)

just what I needed! Thank you.


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!


Steven (view profile)

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.

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.


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?


Brock (view profile)

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

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

Johan Carlson

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

Johan Carlson

Good suggestion. will do!

Martin Huber

very good! please also add color change:

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

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

excellent, thank.



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)

MATLAB Release
MATLAB 7.5 (R2007b)

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

» Watch video