Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB Simple Point Plots

Tags:

plot

matlab

In a while loop, I need to plot the positions (x,y) of two entities. That is, all I need to do is generate a plot with two points on it. I need to scale the plot to a specific maximum x and y value. An additional requirement is the fact that one of the points needs to have three concentric rings placed around it, each with a given radius. Additionally, this all is to happen in a loop, thus I'm hoping that only a single plot window opens and that I don't get a whole slew of windows opening (one for each loop iteration).

Basically here's the pseudo-code I'm trying (and failing!) to implement:

-> Open new plot window, with a given x and y axis
while (running) {
  -> Clear the plot, so figure is nice and clean
  -> Plot the two points
  -> Plot the three circles around point A
}

I found several items in MATLAB's documentation, but no single plotting functions seems to do what I want, or there are instances where I inadvertently create multiple plots with only some of the data (i.e., one plot has the points and another has the circles).

like image 644
It'sPete Avatar asked Dec 02 '25 04:12

It'sPete


2 Answers

here's a sample code you can use in your while loop

x0=1; y0=4; 
x1=2; y1=3;  % the x-y points
r=[1 2 3]; % 3 radii of concentrating rings
ang=0:0.01:2*pi; 
xc=cos(ang)'*r;
yc=sin(ang)'*r;

plot(x0,y0,'.',x1,y1,'.'); % plot the point A
hold on
plot(x1+xc,y1+yc); % plot the 3 circles

% set the limits of the plots (though Matlab does it for you already)
xlim([min([x0 x1])-max(r) max([x0 x1])+max(r)]);
ylim([min([y0 y1])-max(r) max([y0 y1])+max(r)]);

hold off

you can make this work in a loop quite easily, read matlab's documentation on how to do that.

like image 195
bla Avatar answered Dec 05 '25 23:12

bla


Try something like this:

r = [0.25 0.125 0.0625];
d = (1:360) / 180 * pi;
xy_circle = [cos(d)' sin(d)'];
xy_circle_1 = r(1) * xy_circle;
xy_circle_2 = r(2) * xy_circle;
xy_circle_3 = r(3) * xy_circle;

h_plot = plot(0, 0, '.k');
hold on
h_circle_1 = plot(xy_circle_1(:, 1), xy_circle_1(:, 2), '-b');
h_circle_2 = plot(xy_circle_2(:, 1), xy_circle_2(:, 2), '-r');
h_circle_3 = plot(xy_circle_3(:, 1), xy_circle_3(:, 2), '-g');
axis equal

for hh = 1:100
  xy = rand(2, 2) / 4 + 0.375;
  xlim = [0 1];
  ylim = [0 1];
  set(h_plot, 'XData', xy(:, 1));
  set(h_plot, 'YData', xy(:, 2));

  set(gca, 'XLim', xlim)
  set(gca, 'YLim', ylim)

  set(h_circle_1, 'XData', xy_circle_1(:, 1) + xy(1, 1));
  set(h_circle_1, 'YData', xy_circle_1(:, 2) + xy(1, 2));
  set(h_circle_2, 'XData', xy_circle_2(:, 1) + xy(1, 1));
  set(h_circle_2, 'YData', xy_circle_2(:, 2) + xy(1, 2));
  set(h_circle_3, 'XData', xy_circle_3(:, 1) + xy(1, 1));
  set(h_circle_3, 'YData', xy_circle_3(:, 2) + xy(1, 2));

  pause(1)
end

You can change the parameters as you wish.