Adding multiple legends outside for loop which is for multi variables

6 次查看(过去 30 天)
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 中查找有关 Data Distribution Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by