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

Asked by Lisa Justin

### Lisa Justin (view profile)

on 29 Aug 2012
Latest activity Commented on by Kiki

### Kiki (view profile)

on 29 Mar 2016
Accepted Answer by Star Strider

### Star Strider (view profile)

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 (view profile)

Answer by Star Strider

### Star Strider (view profile)

on 29 Aug 2012
Edited by Star Strider

### Star Strider (view profile)

on 29 Aug 2012

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

Kiki

### Kiki (view profile)

on 29 Mar 2016

Hi

What if I have multiple peaks and I want the apply the label respectively? How should I write the text command?

Thank you!

### Ilham Hardy (view profile)

Answer by Ilham Hardy

### Ilham Hardy (view profile)

on 29 Aug 2012

Show 1 older comment
Jan Simon

### Jan Simon (view profile)

on 29 Aug 2012

Then use the output of the peakfinder and a simple TEXT command.

Lisa Justin

### Lisa Justin (view profile)

on 29 Aug 2012

thanks, but what do you mean by simple TEXT command

Jan Simon

### Jan Simon (view profile)

on 29 Aug 2012

See "help text" and "doc text".

### Wayne King (view profile)

Answer by Wayne King

### Wayne King (view profile)

on 29 Aug 2012

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

Lisa Justin

### Lisa Justin (view profile)

on 29 Aug 2012

But the value is not displayed on the plot with Wayne's code

Tom

### Tom (view profile)

on 29 Aug 2012

It should be; can you provide some sample data?

Ilham Hardy

### Ilham Hardy (view profile)

on 29 Aug 2012

Below link is the 'how-to' display tooltip syntactically..

http://undocumentedmatlab.com/blog/controlling-plot-data-tips/

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi