0

I want to plot inside a for loop. I tried the following code also used hold on but the plot is still blank. I don't know where I am getting wrong.

M2 = true(21, 6);
M2(1:2, 3:5) = false;

R = [0.5:0.1:2.5];
H=[0:5:25];
m=21,
n=6,
for i=1:m
    for j=1:n        
        if M(i,j)==0
            plot(H(i),R(j), 'color', 'r')
            drawnow();
        end
    end
end
1
  • 2
    try plot(H(i),R(j), 'r.'). You are plotting individual points. Plot expect lines. Unless you tell it that its a point to draw (the dot after red), it will try to draw a line, but a line with 1 point is invisible Commented Nov 24, 2020 at 11:46

1 Answer 1

1

As Ander Biguri already said, the plot function actually does not draw anything when you provide only one point and do not specify any markers.

But, if you want to create some animated plot, that one of the points is displayed at a time:

h = scatter(0, 0, 'r');
xlim([min(H) max(H)]);
ylim([min(R) max(R)]);
for i=1:m
    for j=1:n
        if M(i,j)==0
            set(h, 'xdata', H(i), 'ydata', R(j));
            pause(0.5);
            drawnow();
        end
    end
end

Or if you want to show all point together, you don't even need a loop:

[HH, RR] = meshgrid(H, R);
scatter(HH(~M), RR(~M), 'r');
xlim([min(H) max(H)]);
ylim([min(R) max(R)]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.