How to set the color in a for loop for a plot within the for loop with an if statement?
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Wolfgang McCormack
el 12 de Mzo. de 2021
Editada: Veronica Taurino
el 12 de Mzo. de 2021
Hi all,
I have the following code but I don't like it. What I want is that we plot the entire t,e but set MarkerFaceColor of each point in the 50 x 50 plot based on the if statements. How should I get that done? when I add the set argument, it does not work at all and only plots a point at 50,50.
for t = 1:50
for e = 1:50
if ismember(t,1:0.01:4.99) & ismember(e,1:0.01:4.99)
plot(t,e,'ok','MarkerFaceColor','k')
hold on
elseif ismember(t,5:0.01:50) & ismember(e,5:0.01:50)
plot(t,e,'or','MarkerFaceColor','r')
hold on
end
end
0 comentarios
Respuesta aceptada
Veronica Taurino
el 12 de Mzo. de 2021
Editada: Veronica Taurino
el 12 de Mzo. de 2021
I don't get very well what you are asking for. You need to color t between 1 and 5 AND e between 1 and 5 in black, otherwise t between 5 (included) and 50 AND e between 5 (included) and 50 in red. With you code, I get this:
What is it wrong according to your needs?
Do you need something like this?
% random entries
t= (50)*rand(50,1);
e= (50)*rand(50,1);
figure
hold on
plot(t,e,'og') % plot all together, just to check if you miss something
for ii = 1:50
if (t(ii)>=1 && t(ii)< 5) && (e(ii)>=1 && e(ii)< 5)
plot(t(ii),e(ii),'.k','MarkerFaceColor','k') %here I used . instead of o, just to check if points fall within the green ones
elseif (t(ii)>=5 && t(ii)<= 50) && (e(ii)>=5 && e(ii)<= 50)
plot(t(ii),e(ii),'.r','MarkerFaceColor','r')
end
end
this is the output (just one point respects the first condition and it is black).
3 comentarios
Veronica Taurino
el 12 de Mzo. de 2021
Editada: Veronica Taurino
el 12 de Mzo. de 2021
To show the labels/values, you need sometyhing like:
text(t,e,strcat('(',num2str(t),',',num2str(e,2),')'),...
'horiz','center','vert','bottom')
Here to adjust your plot: Add text descriptions to data points - MATLAB text - MathWorks Italia
I didn't get your first question.
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!