Adding new circle to plot at constant frequency
1 view (last 30 days)
Show older comments
Hello,
I am trying to plot figures that have a circle inside it. I want to add a new circle to the figure at a constant frequency. let us say each 20 steps. For example, if I have 60 steps then I should have 60 figures. Figures from 1 to 20 will have one circle, figures from 21 to 40 will have 2 circles, and figures from 41 to 60 will have three circles. I did that in below code and it is working properly. However, I used if condition in my code. The problem if I will have 1000 steps this mean I need to use (1000/20= 50) if condition and that does not make sense. Is there any sufficient way to do that. This is an example.In my real case, I will have from step 1-20 circle moving at each step from step 21-40 I will have two circles moving and so on. I gave this example to make the situation easy. I have the location (x,y) for all steps stored in one matrix.
function h = circle(x,y,r)
clc;
for i=1:60
figure
if i>=1 && i<20
x=10;
r=0.4;
y=18;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
elseif i>=20 && i<40
x=10;
r=0.4;
y=14;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+2;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
else i>=40 && i<60;
x=10;
r=0.4;
y=10;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+4;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+2;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end
end
end
Figures that I got is like
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164504/image.jpeg)
and
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164505/image.jpeg)
and
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164506/image.jpeg)
%
0 Comments
Accepted Answer
Robert
on 17 May 2016
Edited: Robert
on 18 May 2016
You should put your circle plotting code in a for loop and iterate from 0 to floor(i/20) (or the reverse).
function h = circle % (x,y,r) % shouldn't have these inputs if we don't use them
x = 10;
r = 0.4;
y = 18;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
for ii=1:60 % using ii avoids confusion with complex numbers
figure
axis([0 20 0 20])
for jj = floor(ii/20):-1:0 % backwards to keep colors same as in your example
h = plot(xunit, yunit+2*(jj-1));
hold on
end
hold off
end
end
2 Comments
Robert
on 18 May 2016
Whoops! floor(ii/20) returns 0 for ii in [0,19]. I should have added 1 or iterated down to zero. I've edited my answer.
More Answers (0)
See Also
Categories
Find more on Axis Labels 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!