Can I manipulate the legend?

The following code will plot me 68 markers. Each marker will have a different color depending on the value of I. Is there a way I can get my legend to show all 5 colors ? For now it is just showing the marker for the first 5 plots, and I just need one per color. If you have any idea or other suggestions that can work as well please let me know.
%for ii=1:1:length(x)
if I(ii)==0;
plot(x(ii), y(ii),'bx','LineWidth',2,'MarkerSize',15)
hold on %attached flow
elseif I(ii)==1;
plot(x(ii), y(ii),'yx','LineWidth',2,'MarkerSize',15)
hold on %energized
elseif I(ii)==2;
plot(x(ii), y(ii),'mx','LineWidth',2,'MarkerSize',15)
hold on %highly energized
elseif I(ii)==3;
plot(x(ii), y(ii), 'rx','LineWidth',2,'MarkerSize',15)
hold on %seperated
else
plot(x(ii), y(ii), 'x','LineWidth',2,'MarkerSize',15)
hold on %incipient
end
%end

Réponses (1)

Star Strider
Star Strider le 13 Juil 2015

0 votes

Try this:
L = 100;
I = randi([0 4], 1, L);
x = randn(1,10);
y = randn(1,10);
figure(1)
hold on
for ii=1:1:length(x)
if I(ii)==0;
hl0 = plot(x(ii), y(ii),'bx','LineWidth',2,'MarkerSize',15)
% hold on %attached flow
elseif I(ii)==1;
hl1 = plot(x(ii), y(ii),'yx','LineWidth',2,'MarkerSize',15)
% hold on %energized
elseif I(ii)==2;
hl2 = plot(x(ii), y(ii),'mx','LineWidth',2,'MarkerSize',15)
% hold on %highly energized
elseif I(ii)==3;
hl3 = plot(x(ii), y(ii), 'rx','LineWidth',2,'MarkerSize',15)
% hold on %seperated
else
hl4 = plot(x(ii), y(ii), 'x','LineWidth',2,'MarkerSize',15)
% hold on %incipient
end
end
hold off
legend([hl0, hl1, hl2, hl3, hl4], 'attached flow', 'energized', 'highly energized', 'seperated', 'incipient')

2 commentaires

Coral Reyes
Coral Reyes le 13 Juil 2015
it didn't work . It says
Undefined function or variable 'hl1'
Star Strider
Star Strider le 13 Juil 2015
That simply means that there were no values of I(ii) that were equal to 1. It works if there are — I tested it to be sure.
I don’t have your data, so I can only simulate them and test your code against my simulations.

Connectez-vous pour commenter.

Commenté :

le 13 Juil 2015

Community Treasure Hunt

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

Start Hunting!

Translated by