How to add a legend in a fig, without showing all lines

16 views (last 30 days)
I have an existing figure, where I have several lines, but I only need to see 5 lines in the legend.
Now I need to open the figure and plot two new points within the figure and add the points to the legend, without showing the remaing existing lines.
Can anyone help me with this?
openfig('Plot with peak torsional moment.fig')
hold on
plot(0,0.0475,'k^','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
hold on
plot(0,-0.0483,'kv','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
legend('Mz_m_a_x','Mz_m_i_n','Mz_m_e_a_n','Mean of peak','std of peak','Mz_m_a_x CFD','Mz_m_i_n CFD','Location','northeastoutside')

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2019
openfig('Plot with peak torsional moment.fig')
hold on
h1 = plot(0,0.0475,'k^','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
hold on
h2 = plot(0,-0.0483,'kv','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
legend([h1, h2], 'Mz_m_a_x','Mz_m_i_n','Mz_m_e_a_n','Mean of peak','std of peak','Mz_m_a_x CFD','Mz_m_i_n CFD','Location','northeastoutside')
  3 Comments
Walter Roberson
Walter Roberson on 25 Jan 2019
Unfortunately as outsiders we do not know which five lines of the original figure have the legends. The person who programmed it might have been selective as to which lines they chose.
But you could try:
openfig('Plot with peak torsional moment.fig')
L = legend(); %capture information about existing legends
LL = L.PlotChildren;
hold on
h1 = plot(0,0.0475,'k^','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
hold on
h2 = plot(0,-0.0483,'kv','Markersize',10,'linewidth',2,'MarkerFaceColor','m');
legend([LL; h1; h2], 'Mz_m_a_x','Mz_m_i_n','Mz_m_e_a_n','Mean of peak','std of peak','Mz_m_a_x CFD','Mz_m_i_n CFD','Location','northeastoutside')

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 24 Jan 2019
Edited: Adam Danz on 24 Jan 2019
After you open the figure, before adding additional objects to the plot, turn off auto-updating like this:
legend('AutoUpdate', 'Off')
This assumes the legend already exists. If it doesn't add the legend and include the AutoUpdate-Off command.
Then plot additional objects.
hold on
plot()
  2 Comments
Marie Skytte Thordal
Marie Skytte Thordal on 25 Jan 2019
Thank you for looking into my problem.
Unfortunately this doesn't seem to work for me. This does not include the two new points in the legend, but only shows the next to lines from the existing figure.
Adam Danz
Adam Danz on 25 Jan 2019
You need to turn the AutoUpdate off after you're done adding objects to the legend.
It's best practice to use handles to specify what objects belong in the legend. But if you're opening a figure that already has objects plotted, you won't have those handles. So you should open the figure, execute "hold on", add the lines you want to be included in the legend, then turn AutoUpdate off. Then you can add more objects to the plot that won't appear on the legend.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!