MATLAB: Change position of many legend

legend

hi !!
i have a probleme to plot legend inside a condition
the last condition reset or be superpositoin on the last one
my code is…..
hold on
if(exist ('blockx') == false)
%disp('blockx is not initialized');
else
blockx = blockx(~all(blockx == 0, 2),:);
cdlx=plot3(blockx(:,2),blockx(:,3),blockx(:,4),'mo','MarkerSize',sz,...
'MarkerEdgeColor','m',...
'MarkerFaceColor','m');
legend(cdlx,'Block_X','Location','northeast');
end
hold on
if(exist ('blocky') == false)
%disp('blocky is not initialized');
else
blocky = blocky(~all(blocky == 0, 2),:);
cdly=plot3(blocky(:,2),blocky(:,3),blocky(:,4),'bo','MarkerSize',sz,...
'MarkerEdgeColor','b',...
'MarkerFaceColor','b');
legend(cdly,'Block_Y','Location','northeast');
end
hold on
if(exist ('blockz') == false)
%disp('blockz is not initialized');
else
blockz = blockz(~all(blockz == 0, 2),:);
cdlz=plot3(blockz(:,2),blockz(:,3),blockz(:,4),'go','MarkerSize',sz,...
'MarkerEdgeColor','g',...
'MarkerFaceColor','g');
legend(cdlz,'Block_Z','Location','northeast');
end
hold on
% if ~exist(cdlz)
%
% end
if(exist ('encast') == false)
%disp('encast is not initialized');
else
encast = encast(~all(encast == 0, 2),:);
cdlxyz=plot3(encast(:,2),encast(:,3),encast(:,4),'rs','MarkerSize',sz,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','r');
legend(cdlxyz,'encastrement','Location','northeast');
end
hold on
if(exist ('blockxz') == false)
%disp('blockxz is not initialized');
else
blockxz = blockxz(~all(blockxz == 0, 2),:);
cdlxz=plot3(blockxz(:,2),blockxz(:,3),blockxz(:,4),'yo','MarkerSize',sz,...
'MarkerEdgeColor','y',...
'MarkerFaceColor','y');
legend(cdlxz,'Block_XZ','Location','northeast');
end
hold on
if(exist ('blockxy') == false)
% disp('blockxy is not initialized');
else
blockxy = blockxy(~all(blockxy == 0, 2),:);
cdlxy=plot3(blockxy(:,2),blockxy(:,3),blockxy(:,4),'co','MarkerSize',sz,...
'MarkerEdgeColor','c',...
'MarkerFaceColor','c');
legend(cdlxy,'Block_XY','Location','northeast');
end
hold on
if(exist ('blockyz') == false)
%disp('blockyz is not initialized');
else
blockyz = blockyz(~all(blockyz == 0, 2),:);
cdlyz=plot3(blockyz(:,2),blockyz(:,3),blockyz(:,4),'ko','MarkerSize',sz,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','k');
legend(cdlyz,'Block_YZ','Location','northeast');
end
hold on
need some help
and thanks

Best Answer

Instead of using a number of individual plot() followed by individual legend() calls, set the plot DisplayName property to the string you want displayed, and legend show once afterwards.
cdlyz=plot3(blockyz(:,2),blockyz(:,3),blockyz(:,4),'ko','MarkerSize',sz,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','k', 'DisplayName', 'Block_YZ');
%and do not legend() right afterwards, not until all of the plots are done