MATLAB Answers

## Best way of drawing multiple circles and updating their position

Asked by Raldi

### Raldi (view profile)

on 12 May 2018
Latest activity Answered by Walter Roberson

### Walter Roberson (view profile)

on 12 May 2018
Accepted Answer by Ameer Hamza

### Ameer Hamza (view profile)

What is the best way to draw a variable number of circles in a plot and update their position?
I have tried
F = rectangle('Position', [(Xpositions - radius) ...
(Ypositions - radius) radius*2 radius*2], ...
'Curvature', [1, 1], 'LineStyle', '--', ...
'EdgeColor', 'r', 'LineWidth', 3);
but since I have multiple circles I would have to create a for loop to draw each circle.
F = viscircles([Xpositions, Ypositions], ...
radii, 'LineStyle', '--', 'EdgeColor', 'r', 'LineWidth', 3);
seems to work fine and draw the circles, but it does not provide of a way to update the positions of the circles.

KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 12 May 2018
Are you looking for co-centric circles?
Raldi

### Raldi (view profile)

on 12 May 2018
No in fact they have different positions in the plot

Sign in to comment.

R2016b

## 3 Answers

### Ameer Hamza (view profile)

Answer by Ameer Hamza

### Ameer Hamza (view profile)

on 12 May 2018
Edited by Ameer Hamza

### Ameer Hamza (view profile)

on 12 May 2018
Accepted Answer

The easiest is to use rectangle() and use handle F to update the Position property
F.Position = [x y r r]; % specify a new psition
F.Position = [x_ y_ r_ r_]; % specify a new psition
everytime you update Position property, the figure will be updated with the new position.
In older MATLAB version use
set(F, 'Position', [x y r r])
In case of for loop, create an array of handles,
% inside for loop
F(i) = rectangle('Position', [(Xpositions - radius) ...
(Ypositions - radius) radius*2 radius*2], ...
'Curvature', [1, 1], 'LineStyle', '--', ...
'EdgeColor', 'r', 'LineWidth', 3);
and then set the property using F(1), F(2), ... instead of F to change a particular circle.

Raldi

### Raldi (view profile)

on 12 May 2018
Yes I have already tried using rectangle() as stated in my question, but in order to draw multiple circles I would have to make multiple draw calls with a for loop if I am not mistaken.
Raldi

### Raldi (view profile)

on 12 May 2018
So there is no way of skipping the for loop? I will keep doing some tests in case I figure something out and let you know, thanks.
Ameer Hamza

### Ameer Hamza (view profile)

on 12 May 2018
Yes, you will need to use for loops. rectangle is a separate graphics object and a call to rectangle() can only create one rectangle. So to draw multiple, a for loop is necessary.
An alternate will be arrayfun, but that is also a fancy way of writing for loop. And in this particular case will not result in any speedup because the actual time-consuming task in your case is the creation of graphics object itself.

Sign in to comment.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 12 May 2018

The easiest way is to use viscircles() in the Image Processing Toolbox.

#### 0 Comments

Sign in to comment.

### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 12 May 2018

You need a loop or arrayfun() to create multiple rectangle. But once you have those, you can unpdate the positions more compactly.
v = [rectangle('Position', [1 2 1 1]); rectangle('Position', [4 3 2 2])]; %create vector of them somehow
newpos = num2cell(horzcat(v.Position)*2,2); %calculate new positions
[v.Position] = deal(newpos{:}); %and... action

#### 0 Comments

Sign in to comment.