MATLAB Answers

Meva
0

Adding multiple legends outside for loop which is for multi variables

Asked by Meva
on 29 Mar 2016
Latest activity Edited by Meva
on 29 Mar 2016
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);

  0 Comments

Sign in to comment.

1 Answer

Answer by Joachim Schlosser on 29 Mar 2016
 Accepted Answer

  1 Comment

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 !

Sign in to comment.