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

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 日

1 投票

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 日

2 投票

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 件のコメント

Lisa Justin
Lisa Justin 2012 年 8 月 29 日
編集済み: Lisa Justin 2012 年 8 月 29 日
no i do not know the values of the peak, i want the value at the peak to also be visible. Same as using the data cursor on a plot directly. it a bunch of signals and i want to run a script on all, i would also like my script to mark the peaks automatically so i do not need to mark each plot before saving
Tom
Tom 2012 年 8 月 29 日
are you looking for one single maximum value, or are you looking for local peaks (i.e. the peaks of a sine wave where there can be several per plot)?
Lisa Justin
Lisa Justin 2012 年 8 月 29 日
one single maximum value
Tom
Tom 2012 年 8 月 29 日
Then Wayne's code above should work fine.
Lisa Justin
Lisa Justin 2012 年 8 月 29 日
But the value is not displayed on the plot with Wayne's code
Tom
Tom 2012 年 8 月 29 日
It should be; can you provide some sample data?
Ilham Hardy
Ilham Hardy 2012 年 8 月 29 日
Below link is the 'how-to' display tooltip syntactically..
Happy reading,

サインインしてコメントする。

Ilham Hardy
Ilham Hardy 2012 年 8 月 29 日

0 投票

4 件のコメント

Lisa Justin
Lisa Justin 2012 年 8 月 29 日
編集済み: Lisa Justin 2012 年 8 月 29 日
I also need the value at that peak to be displayed. Same as using the data cursor on a plot to mark points. but i want the data cursor to mark the peaks in my plot automatically so i do not need to mark it directly
Jan
Jan 2012 年 8 月 29 日
Then use the output of the peakfinder and a simple TEXT command.
Lisa Justin
Lisa Justin 2012 年 8 月 29 日
thanks, but what do you mean by simple TEXT command
Jan
Jan 2012 年 8 月 29 日
See "help text" and "doc text".

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by