How to set the color in a for loop for a plot within the for loop with an if statement?
조회 수: 18 (최근 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
댓글 수: 0
채택된 답변
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
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')
Here to adjust your plot: Add text descriptions to data points - MATLAB text - MathWorks Italia
I didn't get your first question.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!