Adding multiple legends outside for loop which is for multi variables

조회 수: 8 (최근 30일)
Meva
Meva 2016년 3월 29일
편집: Meva 2016년 3월 29일
Hello, I used two different colourbar for two different variables.
The variables are plotted in for loop. Legends should be outside for loop.
I cannot add multiple legend outside for loop.
I know that it must not recognise leg_copy.
However, I need to put all legends outside loop. Here is the code :
hold on
clr = jet(alphasteps); %# 1st colormap for first variable
clr2 = copper(alphasteps); %# 2nd colormap for second variable
%------------------------------------------
for l = 1:alpha steps
... ... ...
end
%------------------------------------------
if exist ('t_clashing', 'var') & exist ('alpha_clashing', 'var')
t_clashing
alpha_clashing
t_vect = 0:dt:max(t_clashing);
figure(5);
for i=1:alphasteps
hf1 = figure('Color', 'w');
ax1 = axes('Parent',hf1);
hold on;
% First set of plots
hplot1=plot(t_vect(1:t_clashing_index(i)-20), H1L(1:t_clashing_index(i)-20, i),'Color',clr(i,:))
hf2 = figure('visible', 'on');
ax2 = axes('Parent',hf2,'visible','on');
hold(ax2, 'on');
set(hplot1,'Parent',ax2);
set(hleg_copy,'Parent',hf2);
hplot2=plot(t_vect(1:t_clashing_index(i)-20),H1R(1:t_clashing_index(i)-20, i),'Color',clr2(i,:))
hold on;
set(gcf, 'Color',[1 1 1]);
set(hplot1, 'lineWidth', 0.8);
set(hplot2, 'lineWidth', 0.8);
end
xlabel('t')
box on
end
end
%------------------------------------------
hold off
[hleg,hobjs,hlinpatches,legtxt] ...
= legend(hplot1,'h_1L , \alpha = -2', 'h_1L , \alpha = -1.9','h_1L , \alpha = -1.8','h_1L , \alpha = -1.7','h_1L , \alpha = -1.6','location','NorthWest');
hleg_copy = copyobj(hleg,hf1);
delete(hleg);
hleg_copy = copyobj(hleg,hf1);
delete(hleg);
[hleg2,hobjs2,hlinpatches2,legtxt2] = ...
legend(hplot2,'h_1R , \alpha = -2', 'h_1R , \alpha = -1.9','h_1R , \alpha = -1.8','h_1R , \alpha = -1.7','h_1R , \alpha = -1.6','location','NorthEast');
% Create a copy of the second legend and delete the original
% This forces similar appearance and behavior for the two legends.
hleg_copy2 = copyobj(hleg2,hf1);
delete(hleg2);
set(hleg_copy,'Tag','', 'ButtonDownFcn','', 'UserData', []);
set(hleg_copy2,'Tag','', 'ButtonDownFcn','', 'UserData', []);
set(hplot1,'Parent',ax1);
set(hplot2, 'Parent', ax1);
set(hleg_copy,'Parent',hf1);
xlabel('X-Data');
close(hf2);

채택된 답변

Joachim Schlosser
Joachim Schlosser 2016년 3월 29일
  댓글 수: 1
Meva
Meva 2016년 3월 29일
편집: Meva 2016년 3월 29일
I have used that code from that answer. However, my case is different! I have to put my legend outside the loop. That is why, it does not recognise hleg_copy !

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

추가 답변 (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