Labelling the concentric circles

13 views (last 30 days)
HAINGKHARRAN GUNASEKARAN
HAINGKHARRAN GUNASEKARAN on 28 Jun 2021
Answered: Suraj Kumar on 15 Feb 2024
Hi there. My aim is to plot multiple concentric circles at different locations on a picture. The following coding was used to accomplish it. My problem is I want to label each circle with a value on the legend. So each circle will have different colour and the legend will show the value. It would be better if i could label it on the cirlce itself. I tried to do it with the legend command, but it didn't work. Can anyone help me out? Thank you.
The coding are as follows:
I = imread('aaaa.png') ;
figure(1)
imshow(I)
th = linspace(0,2*pi) ;
hold all
while true
hold all
message = sprintf('Click where you want the center to be');
uiwait(helpdlg(message));
[xCenter, yCenter] = ginput(1);
X=[5,30,100];
for radius =(X)
x = radius * cos(th) + xCenter;
y = radius * sin(th) + yCenter;
plot(x, y, '-');
title('Risk Contour')
end
legend(input('array form'),'AutoUpdate','off')
hold all
j = input(' Stop and proceed? (input: "y" - yes (stop) or "n" - no (if want to plot another contour))\n','s');
if j=='n'
continue
elseif j=='y'
break
else
disp('Error:Input should be ''y'' or ''n''\n')
end
end

Answers (1)

Suraj Kumar
Suraj Kumar on 15 Feb 2024
Hi HAINGKHARRAN ,
To accomplish your requirement of plotting multiple concentric circles at different locations and editing the labels manually , I made a few modifications to your code:
  1. Hold on is used instead of hold all as it is deprecated.
  2. The user is now provided with graphical dialog box instead using the command line for confirmation.
  3. Ability to manually edit the label of the circles is added. To achieve the functionality of editing the label of each circle manually, you can use a callback function which will helps to edit the text by allowing user to input the label in a dialog box.
You can refer to the following code snippet-:
I = imread('aaaa.png');
figure(1)
imshow(I)
th = linspace(0, 2*pi);
hold on
while true
message = sprintf('Click where you want the center to be');
uiwait(helpdlg(message));
[xCenter, yCenter] = ginput(1);
% Check if the user pressed "Enter" without clicking
if isempty(xCenter)
break;
end
X = [5, 30, 100]; % Radius of the concentric circles defined
for radius = X
x = radius * cos(th) + xCenter;
y = radius * sin(th) + yCenter;
h = plot(x, y, '-');
title('Risk Contour')
% Label the circle with its radius and make the label editable
labelStr = sprintf('%.2f', radius);
textHandle = text(xCenter, yCenter - radius, labelStr, ...
'Color', h.Color, ...
'VerticalAlignment', 'bottom', ...
'HorizontalAlignment', 'center', ...
'BackgroundColor', 'w', ...
'Margin', 1, ...
'FontWeight', 'bold', ...
'ButtonDownFcn', @editText); % Add callback for editing
end
% Ask the user if they want to continue or stop
choice = questdlg('Do you want to plot another contour?', ...
'Continue', 'Yes', 'No', 'Yes');
if strcmp(choice, 'No')
break;
end
end
% Callback function for editing text labels
function editText(src, ~)
% Prompt user for new label
newLabel = inputdlg('Enter new label:', 'Edit Label', 1, {src.String});
if ~isempty(newLabel)
src.String = newLabel{1}; % Update the label
end
end
You might also find these references useful:
  1. https://www.mathworks.com/help/matlab/ref/questdlg.html
  2. https://www.mathworks.com/help/matlab/creating_plots/create-callbacks-for-graphics-objects.html
Hope this helps.

Community Treasure Hunt

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

Start Hunting!