필터 지우기
필터 지우기

How do I put annotation text pointing towards specific coordinate?

조회 수: 3 (최근 30일)
Below is my code , I have tried to put annotation text pointing towards a coordinate(3.4, 4.8248) using trial and error method . What is the proper way ?
x=[1 2 2.5 3 4 5]
x = 1×6
1.0000 2.0000 2.5000 3.0000 4.0000 5.0000
y=[0 5 7 6.5 2 0]
y = 1×6
0 5.0000 7.0000 6.5000 2.0000 0
A=3.4;Lagrange(x,y,A)
A = 3.4000
s = 0
c = 1×2 cell array
{[3.4000]} {[4.8248]}
ans = 4.8248
function yint=Lagrange(x,y,xx)
A=3.4
n=length(x);
s=0
for i=1:n
product=y(i);
for j=1:n
if i~=j
product=product*(xx-x(j))/(x(i)-x(j));
end
end
s=s+product;
end
yint=s;
figure()
plot(x,y)
hold on
scatter(A,yint,"x","b")
scatter(x,y,"o")
title('Lagrange Interpolation'); xlabel('x'); ylabel('f(x)');
c={A yint}
annotation('textarrow',[0.7 0.6],[0.7 0.64],'String',c)
hold off
legend('Interpolated Function','Result Point f(3.4)','Given Data');
hold off
end

채택된 답변

Matt J
Matt J 2021년 11월 9일
편집: Matt J 2021년 11월 10일
x=[1 2 2.5 3 4 5];
y=[0 5 7 6.5 2 0];
A=3.4;Lagrange(x,y,A);
function yint=Lagrange(x,y,xx)
A=3.4;
n=length(x);
s=0;
for i=1:n
product=y(i);
for j=1:n
if i~=j
product=product*(xx-x(j))/(x(i)-x(j));
end
end
s=s+product;
end
yint=s;
figure()
plot(x,y)
hold on
scatter(A,yint,"x","b")
scatter(x,y,"o")
title('Lagrange Interpolation'); xlabel('x'); ylabel('f(x)');
c={A yint};
%%%%%Matt J added
ax=gca;
xl=xlim;
yl=ylim;
xy0=[xl(1),yl(1)];
Dxy=[diff(xl),diff(yl)];
fn=@(xy) (xy(:)'-xy0)./Dxy.*ax.InnerPosition([3,4])+ax.InnerPosition([1,2]);
p=fn([3.4, 4.8248]);
%%%%%%
annotation('textarrow',[0.7 p(1)],[0.7 p(2)],'String',c)
hold off
legend('Interpolated Function','Result Point f(3.4)','Given Data');
hold off
end

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 11월 9일
Here you can use the values of c to place where you want your annotation to be displayed along with the arrow. E.g.:
...
c={A yint}
annotation('textarrow',[c{1,1}, c{1,1}-0.07*max(x)]/max(x),[c{1,2},c{1,2}-0.07*max(y)]/max(y),'String',c)
...
  댓글 수: 3
Matt J
Matt J 2021년 11월 9일
Sulaymon Eshkabilov's comment moved here
WIth the proposed solution, you don't need to find the coordinates by a trial and error. It takes up the found values of c. In other words, the annotation placement changes with respect to the calculated values of c.
Samson David Puthenpeedika
Samson David Puthenpeedika 2021년 11월 10일
How to use values of c in the above code? When i tried values of 'c' it was giving error that values should be between 0-1

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by