Matlab 2018 - Figure legend swamped by data2, data3, data4,... used to be able to just put single legend entry.. how to revert to previous behavior?

2 views (last 30 days)
In updgrading to Matlab 2018 all my figure legends are broken. I use to use legend once for many lines from the same data. Current matlab legend help states "The legend automatically updates when you add or delete data series from the axes". This is true as now my legends are swamped even if I only call the legend after plotting a single line. How to have a limited number of legend labels, say 2, even when plotting 100's of lines on a figure.
A snippet of ugly code that used to work well for me, but now generates line 1 of a legend as I want and then data2, data3, ... datan..
for i=1:length(ids)
plot(D{ids(i)},V{ids(i)},'k')
if i==1
hold on
legend('WSP','Location','Northwest')
end
end
hold off
Any help appreciated.

Accepted Answer

Jan
Jan on 31 Jan 2019
Edited: Jan on 31 Jan 2019
Do you mean:
legend('WSP', 'Location', 'Northwest', 'AutoUpdate', 'off')
  2 Comments
Benjamin Schafer
Benjamin Schafer on 31 Jan 2019
Excellent, thank you. will add that to all my new files that are having this problem.
Can the default legend propterty be changed in the beginning of a script so that my old code would still work without modification? (I know this is asking a lot.. )

Sign in to comment.

More Answers (1)

Tolga Karabiyikoglu
Tolga Karabiyikoglu on 23 Jan 2025
Here is another option for you,
First define handles for the plots you want to see on your legend. It would be easy if you have a loop to generate your plots on the same axes.
Next continue your ploting code with a snippet as follows;
legend(HandleOfTheAxes, [HandleOfThePlotsToLegend], 'Location', 'northeast'); or issue a line like
legend(gca, [HandleOfThePlotsToLegend], 'Location', 'northeast');
as you donot have defined a handle to your axes before...

Tags

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!