Marking the peak of a plot automatically without having to click on it

71 次查看(过去 30 天)
Hi,
Is it possible to mark the peak of a plot automatically with a command without having to to mark on each plot.

采纳的回答

Star Strider
Star Strider 2012-8-29
编辑:Star Strider 2012-8-29
If you have the Signal Processing Toobox, I suggest the findpeaks function with two outputs so that you get the value of the function at the peak as well as the index of your x-variable at which the peak occurs.
For example:
[Peak, PeakIdx] = findpeaks(Y);
and the label would then be:
text(x(PeakIdx), Peak, sprintf('Peak = %6.3f', Peak));
using the text command.
To illustrate:
X = [0:0.1:pi]';
Y = sin(X);
[Peak, PeakIdx] = findpeaks(Y);
figure(1)
plot(X, Y)
text(X(PeakIdx), Peak, sprintf('Peak = %6.3f', Peak))
  1 个评论
Kiki
Kiki 2016-3-29
Hi
What if I have multiple peaks and I want the apply the label respectively? How should I write the text command?
Thank you!

请先登录,再进行评论。

更多回答(3 个)

Wayne King
Wayne King 2012-8-29
Do you know the value of the peak?, then yes, it's easy.
x = randn(100,1);
[val,I] = max(x);
plot(x); hold on;
plot(I,val,'r^','markerfacecolor',[1 0 0])
  7 个评论

请先登录,再进行评论。


Ilham Hardy
Ilham Hardy 2012-8-29
  4 个评论

请先登录,再进行评论。


Sergio Yanez-Pagans
You could use my MATLAB File Exchange function:
Hope you find this useful!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by