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

조회 수: 31 (최근 30일)
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

채택된 답변

Veronica Taurino
Veronica Taurino 2021년 3월 12일
편집: Veronica Taurino 2021년 3월 12일
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
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 12일
@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 2021년 3월 12일
편집: Veronica Taurino 2021년 3월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by