MATLAB: How to hide or delete the legend that is not needed

hidelegend

FYI, please see the attached figure.
My aim is to hide or delete the legend data1, data2 and data3, to be like this figure.
Thanks!

Best Answer

Use line objects as inputs to legend, telling it which lines to include in the legend.
x=1:10;
y=@(x,a)((x-5)/a).^2;
p1 = plot(x,y(x,1),'DisplayName','Mode 1,Analytical,UD');
hold on
plot(x,y(x,1.1));
p2 = plot(x,y(x,2),'DisplayName','Mode 2,Analytical,UD');
plot(x,y(x,2.2));
p3 = plot(x,y(x,3),'DisplayName','Mode 3,Analytical,UD');
plot(x,y(x,3.3))
hold off
legend([p1,p2,p3])
Related Question