Using 2 values on for loop doesn't work correct for me
Show older comments
x = input('enter x number:')
y = input('enter y number:')
z = input('enter widht:')
v = input('enter lenght:')
axes('NextPlot', 'add','XLim', [(-abs(x)-abs(z)), (abs(x)+abs(z))], 'YLim', [(-abs(y)-abs(v)), (abs(y)+abs(v))]);
for e = x:0.5:v
plot(e, 0, 'o')
pause(0.15)
end
for r = y:0.5:z
plot(r, 0, 'o')
pause(0.15)
end
I am trying to plot a rectangle. But when I wrote something like this to end of the code
for r = 0:0.5:12, e = 0:0.5:10;
plot(e, r, 'o')
pause(0.15)
end
I got a weird output. If I try to make square, square is full with 'o'. I don't want it to be full of 'o'. I just want to get empty rectangle or square. I couldn't get a right output. How can I do that?
Answers (1)
Cute.
What is the purpose of
for r = 0:0.5:12, e = 0:0.5:10;
? This runs a for loop and creates a vector e with the value 0:0.5:10 in each iteration. Do you expect that Matlab can imagine, that you want to do something else? Matlab's capabilities to read your mind are very limited :-)
You try to create a square full of o's. So use nested loops:
for r = 0:0.5:12
for e = 0:0.5:10
plot(e, r, 'o')
pause(0.15)
end
end
In the last days we find a lot of questions in the forum concerning the creation of squares. Many of them use stars instead of o's.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!