Labelling the concentric circles
Mostrar comentarios más antiguos
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
Respuestas (1)
Suraj Kumar
el 15 de Feb. de 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:
- Hold on is used instead of hold all as it is deprecated.
- The user is now provided with graphical dialog box instead using the command line for confirmation.
- 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:
- https://www.mathworks.com/help/matlab/ref/questdlg.html
- https://www.mathworks.com/help/matlab/creating_plots/create-callbacks-for-graphics-objects.html
Hope this helps.
Categorías
Más información sobre Data Exploration en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!