# How to label the centers of circles according to their position on axes?

6 views (last 30 days)

Show older comments

Shailly Vaidya
on 23 Aug 2021

Edited: Shailly Vaidya
on 24 Aug 2021

Hello all.

I have found and masked circles in an image. Now, I wish to label the circles as 1, 2, 3....end (please see the attached image) , and I want the numbers to appear at the center of the circles. The attached image is how I want the output to be.

Tried implementing it using text(). I used the following code which definitely has errors; as it is not giving the desired output.

for i=1:sorted_centers_x % sorted_centers_x is an array with all the X coordinates of the centers of the circles

for j=1:sorted_centers_y % sorted_centers_y is an array with all the Y coordinates of the centers of the circles

if (sorted_centers_y(j) >=0) && (sorted_centers_y(j)<20) % iterating with different range of values. so as to label 1-6 on the circles in the upper row and so on..

text(double(sorted_centers_x(j)),double(sorted_centers_y(i)),num2str(j),'Color','g','FontSize', 14,'FontWeight','bold');

else

text(double(sorted_centers_x(j)),double(sorted_centers_y(i)),num2str(j),'Color','g','FontSize', 14,'FontWeight','bold');

end

end

end

Any help would be appreciated! Thanks in advance :)

##### 0 Comments

### Accepted Answer

Simon Chan
on 23 Aug 2021

Try this and still using function text:

clear; clc;

I = ones(400,600);

sorted_centers_x = repmat(50:100:550,1,4);

sorted_centers_y = repelem(50:100:350,1,6);

imshow(I)

text(sorted_centers_x,sorted_centers_y,num2str([1:24]'))

### More Answers (1)

Devyani Maladkar
on 23 Aug 2021

Hello,

It is my understanding that you want to label the masked circle by placing a number at the centre of the circle.

The text function can be used to insert text at the desired position in the current axes. The documentation here can be used to understand more about the text function use.

I have provided the code below for the implementation of labelling the circles with appropriate numbers. I have used sample data to demonstrate that the numbering takes place depending on where the (0,0) reference point is.

In the below code the reference point (0,0) is in the bottom-left corner of the plot, and the circle with maximum y-coordinate and minimum x-coordinate gets labelled 1 (upper-left corner). The labelling proceeds with increasing x-coordinate and decreasing y-coordinate hence the for-loop iteration is on ascending x values and descending y values.

%Sample data

sorted_centers_x=sort([0.1 ,0.2,0.3,0.4,0.5],'ascend')

sorted_centers_y=sort([0.1,0.2,0.3],'descend')

for j=1:numel(sorted_centers_y) % sorted_centers_x is an array with all the X coordinates of the centers of the circles

for i=1:numel(sorted_centers_x) % sorted_centers_y is an array with all the Y coordinates of the centers of the circles

if (sorted_centers_y(j) >=0) && (sorted_centers_y(j)<20) % iterating with different range of values. so as to label 1-6 on the circles in the upper row and so on..

text(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','g','FontSize', 14,'FontWeight','bold');

else

ext(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','r','FontSize', 14,'FontWeight','bold');

end

end

end

##### 2 Comments

Devyani Maladkar
on 23 Aug 2021

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!