How do I plot individual circles whose centers are points on a graph?

I generated some random numbers which represent random forces acting on a spring, I then divided this random forces by a spring constant 20.5 to get the random positions (using Hooke's Law), I selected the random positions between -50 and 50. This random positions were then plotted (using the code below);
x_max=50.0;
x_min=-50.0;
N = 100;
RandFor = 1030*rand(N,1) .* sign(randn(N,1));
k= 20.5;
x=RandFor./k;
x(x_min > x | x > x_max) = 0;
scatter( 1:length(x), x, 75,[0 0 0],'filled' )
plot(x)
figure(1)
%subplot(1,3,1:2)
xlabel('frames');
ylabel('Random Positions');
I need to plot individual circles centered on each plotted random position, thus for above code there should be 100 circles. These circles are one dimensional because the random positions are plotted only on the y-axis. so the circles have only y co-ordinates, the x-axis represents the captured frame of each circle.
I tried using the code below to plot the circle
function circle(a,b,r)
%x and y are the coordinates of the center of the circle
%r is the radius of the circle
%0.01 is the angle step,
ang=0:0.01:2*pi;
xp=r*cos(ang);
yp=r*sin(ang);
plot(a+xp,b+yp);
hold on;
end
But I don't know how to make the circle appear on each random position.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de En. de 2013
You can use scatter() to plot circles. The third parameter to scatter() is the size of the circle.

9 comentarios

Thank you for answering my question.How do I use the scatter function to plot a circle on each position plotted? my goal is to make circles appear on each position at a time (I believe I can use append to accomplish this) and then generates frames of a circle at different location
scatter(X, Y, Size, Color)
X and Y can be vectors. Size can be a scalar or can be a vector the same size as X (and Y). Color can be a color string or an RGB vector or an RGB array (one row of RGB per value of X).
Your code has
if x_min< x < x_max
This will be interpreted as
if ((x_xmin < x) < x_max)
The first part, x_xmin < x, will return a logical vector of results, 0 for false and 1 for true. Those logical results will then be tested for < x_max . 0 and 1 are both always less than +50, so the result of that second comparison will be a logical vector composed entirely of true results (all 1s). When "if" checks a logical vector, it considers the result to be true if every member of the vector is non-zero, which in this situation it would be. Therefore your "if" is always true, so dx will be set to x.
And then it will not matter what happened in the "if" because you never use dx !!
If you want to test whether a scalar is in a range, you should test
if x_min <= x & x <= x_max
If you want to select the elements of a vector that are in a particular range, then you should use logical indexing, such as
x(x_min > x | x > x_max) = 0;
Now, when you finally have your "x" variable, try
scatter( 1:length(x), x, 75 )
where 75 is the circle size parameter (which you might need to adjust)
Ok Thank you very much, I really appreciate your help. I am new to MatLAB that is why I have all these errors. So using the scatter plot will I be able to generate some images showing each individual circle different positions on the graph
Could you please give me an idea how to do this?
EngStudent
EngStudent el 24 de En. de 2013
Editada: EngStudent el 24 de En. de 2013
I want to generate frames and each frame will show the position of the circle a different point
Consider circle #17 (for example.) In frame #2, should only the new position of circle #17 be shown, or should both the old and new positions be shown?
If you want the positions to accumulate on the same graph, then just ensure you have done
hold on
after doing the first scatter().
I want the new position on frame #2, thus I want each circle to have its own frames, I will then develop a program to track the positions of the circle in each frame, these tracked positions will then be compared with the generated positons

Iniciar sesión para comentar.

Más respuestas (1)

disha
disha el 5 de Abr. de 2013
You can try the following modification in your code. plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) Hope this helps.

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by