How do I copy over only one legend entry from each plot using copyobj()?

조회 수: 15 (최근 30일)
I have three separate figure files that consist of multiple plots, but with only one legend entry each. This is because each figure consists of multiple lines of the same color.
In order to overlay each of these figures into a single figure, I input:
% Load saved figures
hgload('design_1.fig');
hgload('design_2.fig');
hgload('baseline.fig');
% Identify figures
fig1 = findobj(1,'Type','Axes');
leg1 = legend(fig1,'Design 1');
fig2 = findobj(2,'Type','Line');
leg2 = legend(fig2,'Design 2');
fig3 = findobj(3,'Type','Line');
leg3 = legend(fig3,'Baseline');
% Combine figures
copyobj(fig2,fig1);
copyobj(fig3,fig1);
However, when I do this, the legend has many extraneous entries corresponding to the lines that should not be labeled. I want my legend to only have 3 entries, exactly one from each figure. Any advice?

채택된 답변

Neil Guertin
Neil Guertin 2018년 1월 5일
When creating a legend, you can specify exactly the objects you want to appear in it. In this case, you will want to recreate the legend with just one line from each series.
% open figures;
fig1 = openfig('design_1.fig');
fig2 = openfig('design_2.fig');
fig3 = openfig('baseline.fig');
% get handles to axes and lines
ax = findobj(fig1,'Type','Axes');
p1 = findall(fig1,'Type','Line');
p2 = findall(fig2,'Type','Line');
p3 = findall(fig3,'Type','Line');
% copy lines to same figure
p2_new = copyobj(p2,ax);
p3_new = copyobj(p3,ax);
% recreate legend
legend(ax,[p1(1),p2_new(1),p3_new(1)])
  댓글 수: 1
Tim Chau
Tim Chau 2018년 1월 6일
I found a workaround by employing a dummy figure, but this is much more elegant. I figured (no pun intended) that there was a correct way to do this; thank you so much for your answer!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by