I'm surprised this hasn't got more good ratings and comments.
May I suggest you also copy the LineWidth and MarkerSize properties from the original plot to the markers. Currently you only copy the LineStyle and Color. Even if there are no markers in the original plot the MarkerSize property can still be set.
Thanks for the update. A couple of minor issues. The function won't cope with more than 13 plots, and also fails if called with N=1. There's not much you can do about this but a clear error message for these cases would be an improvement and would have saved me about 10 seconds :-)
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!
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.
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.
/JC
Comment only