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

plotting

Hi,
Is it possible to mark the peak of a plot automatically with a command without having to to mark on each plot.

Best Answer

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))