How to set the color in a for loop for a plot within the for loop with an if statement?

18 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Veronica Taurino
Veronica Taurino am 12 Mär. 2021
Bearbeitet: Veronica Taurino am 12 Mär. 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 Kommentare
Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021
@Veronica Taurino sorry quick question, how can I create some white space between the start of the axis on both x and y and on the end as well. Also, is it possible to show the values on each dot?
Thanks in advance. I really appreciate it.
Veronica Taurino
Veronica Taurino am 12 Mär. 2021
Bearbeitet: Veronica Taurino am 12 Mär. 2021
To show the labels/values, you need sometyhing like:
text(t,e,strcat('(',num2str(t),',',num2str(e,2),')'),...
'horiz','center','vert','bottom')
I didn't get your first question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by