legend for circle matlab

How can i add the legend for "red circle" in the image? The circle was generated using the below syntax: (I already tried this syntax , but failed)
%%INPUT DATA
clear;clc;
R = 10; x_c = 5; y_c = 8;thetas = 0:pi/64:pi;
xs = x_c + R*cos(thetas); ys = y_c + R*sin(thetas);%create input data
% Now add some random noise to make the problem a bit more challenging:
mult = 0.5;
xs = xs+mult*randn(size(xs));
ys = ys+mult*randn(size(ys));
xfit=4.9641;yfit=8.1215;Rfit=9.8862;
%%PLOT RESULT
figure; plot(xs,ys,'b.');%plot data
rectangle('position',[xfit-Rfit,yfit-Rfit,Rfit*2,Rfit*2],'curvature',[1,1],'linestyle','-','edgecolor','r');
hold on;title(sprintf('Best fit: Radius = %0.1f; Center = (%0.3f,%0.3f)',Rfit,xfit,yfit));%create title
plot(xfit,yfit,'g.');xlim([xfit-Rfit-2,xfit+Rfit+2]);ylim([yfit-Rfit-2,yfit+Rfit+2]);axis equal;
legend('input data','center circle','fitted circle');
Hope the result as below:
THANKS,

 Risposta accettata

madhan ravi
madhan ravi il 21 Ott 2018
Modificato: madhan ravi il 21 Ott 2018
R = 10; x_c = 5; y_c = 8;thetas = 0:pi/64:pi;
xs = x_c + R*cos(thetas); ys = y_c + R*sin(thetas);%create input data
% Now add some random noise to make the problem a bit more challenging:
mult = 0.5;
xs = xs+mult*randn(size(xs));
ys = ys+mult*randn(size(ys));
xfit=4.9641;yfit=8.1215;Rfit=9.8862;
figure(1);
h1=plot(xs,ys,'b.');%plot data
hold on;title(sprintf('Best fit: Radius = %0.1f; Center = (%0.3f,%0.3f)',Rfit,xfit,yfit));%create title
h2=plot(xfit,yfit,'g.');xlim([xfit-Rfit-2,xfit+Rfit+2]);ylim([yfit-Rfit-2,yfit+Rfit+2]);axis equal;
h3=rectangle('position',[xfit-Rfit,yfit-Rfit,Rfit*2,Rfit*2],'curvature',[1,1],'linestyle','-','edgecolor','r');
syms x y %symbolic toolbox
h=fimplicit((x-xfit).^2+(y-yfit).^2-Rfit.^2)
h.Color='r'
axis equal
legend([h1 h2 h],{'input data','center circle','fitted circle'})

5 Commenti

madhan ravi
madhan ravi il 21 Ott 2018
its not possible to create an array of legend handles when geometric shape command instance rectangle is used
ha ha
ha ha il 21 Ott 2018
please see edit code. I tried to use your recommendation, but it is NOT working.
madhan ravi
madhan ravi il 21 Ott 2018
Modificato: madhan ravi il 21 Ott 2018
when I ran your code I didn't get any plots , are you sure you get those plots with your code??
ha ha
ha ha il 21 Ott 2018
Modificato: ha ha il 21 Ott 2018
plz, check the code again. I already revised it. How can I add the legend for the "red circle line" as expectation figure?
madhan ravi
madhan ravi il 21 Ott 2018
see my edited answer

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 21 Ott 2018

0 voti

Sorry, but you're not doing a fit of your data to a circle at all.
You're just drawing a circle of a predefined size.

Richiesto:

il 21 Ott 2018

Commentato:

il 22 Ott 2018

Community Treasure Hunt

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

Start Hunting!

Translated by