Displaying a value in the plot

조회 수: 22 (최근 30일)
Daniel
Daniel 2014년 10월 4일
편집: dpb 2019년 10월 7일
Hello,
I am plotting x vs y and I am taking out the maximum value of x with max(x)
Now I want to display this maximum value of x together with some text in a legend. I am trying to use num2str.. I am not sure if I am the right track.

채택된 답변

Star Strider
Star Strider 2014년 10월 4일
I would use sprintf, for example:
x = linspace(-5,5);
y = 5-(x+2).^2;
[maxy,idx] = max(y);
figure(1)
plot(x, y, x(idx),y(idx),'pr')
legend('y(x)', sprintf('Maximum y = %0.3f',maxy))
axis([xlim min(y) max(y)+1])
  댓글 수: 2
Ivan Mich
Ivan Mich 2019년 10월 6일
편집: Ivan Mich 2019년 10월 6일
Exsuse me, but in this example we want one value, the maxy. If we want to put min(y) too (which means 2 values) what changes of this command we shall do?
Star Strider
Star Strider 2019년 10월 6일
편집: dpb 2019년 10월 7일
@Ravnalis Michael —
Either change the call from max to min (and the rest of the code appropriately), or add a separate min call, similar to the max call.

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

추가 답변 (1개)

dpb
dpb 2014년 10월 4일
Well, we don't know if you're "on the right track" or not, either since you didn't show what you tried nor indicate what the problem was, if any...
num2str is fine (as would be sprintf) to use to format a value for display with additional text using the format string. To display it, you use text
You can also use annotation object for an arrow pointing to the value and so on.
doc text % for details

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by