Hello, I am creating a graph with mutiple for loops to obtain data, the problem at the moment is when I try to creat a legend corresponding to each specific value the it is only seeing the first for loop.
figure, hold on
for i=1:size(AB,1)
a1 = VAB(:,1)'*AB(i,:)';
b1 = VAB(:,2)'*AB(i,:)';
plot(a1,b1,'ko','LineWidth',3)
end
for i=1:size(CD,1)
c1 = VCD(:,1)'*CD(i,:)';
d1 = VCD(:,2)'*CD(i,:)';
plot(c1,d1,'bo','LineWidth',3)
end
for i=1:size(EF,1)
e1 = VEF(:,1)'*EF(i,:)';
f1 = VEF(:,2)'*EF(i,:)';
plot(e1,f1,'go','LineWidth',3)
end
for i=1:size(GH,1)
g1 = VGH(:,1)'*GH(i,:)';
h1 = VGH(:,2)'*GH(i,:)';
plot(g1,h1,'mo','LineWidth',3)
end
xlabel('PC1'),ylabel('PC2')
grid on, set(gca,'FontSize',15)
axis ([-1 1 -.1 .1])
legend('CAP','Capsiate','RTX','CPZ')

 Accepted Answer

For every plot call, return a handle:
h1 = plot(a1,b1,'ko','LineWidth',3);
. . .
h2 = plot(c1,d1,'bo','LineWidth',3);
. . .
h3 = plot(e1,f1,'go','LineWidth',3);
. . .
h4 = plot(g1,h1,'mo','LineWidth',3);
then in the legend call:
legend([h1 h2 h3 h4],'CAP','Capsiate','RTX','CPZ')
That should do what you want.
I cannot test it with your code, so I am posting this as UNTESTED CODE. It should work.

2 Comments

That worked perfectly, thank you so much!!!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!