File Exchange

image thumbnail


version (272 KB) by Tom O'Haver
Keypress operated interactive peak detector function.


Updated 04 May 2016

View Version History

View License

iPeak is a keyboard-operated Interactive Peak Finder for time series
data. Expected input forms:
ipeak(y); % Data in single y vector')
ipeak(x,y); % Data in separate x and y vectors')
ipeak(DataMatrix); % Data in two columns of DataMatrix')
ipeak(x,y,10), ipeak([x;y],10) or ipeak(y,10), specifying peak density, PeakD
ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW) specifying peak density, AmpT, SlopeT, SmoothW, FitW')
ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange) Adding pan and zoom settings ')
ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange,Autozero) Adding Autozeo as 9th argument')
ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange,MaxError,positions,names) Adding peak ID')

EXAMPLE 1: One input argument; data in single vector
>> y=cos(.1:.1:100);ipeak(y)

EXAMPLE 2: One input argument; data in two columns of a matrix
>> x=[0:.01:5]';y=x.*sin(x.^2).^2;M=[x y];ipeak(M);

EXAMPLE 3: Two input arguments; data in separate x and y vectors
>> x=[0:.1:100];y=(x.*sin(x)).^2;ipeak(x,y);

EXAMPLE 4: Additional input argument (after the data) to control peak
>> x=[0:.1:100];y=5+5.*cos(x)+randn(size(x));ipeak(x,y,10);
or >> ipeak([x;y],10); or >> ipeak(humps(0:.01:2),3)
or >> x=[0:.1:10];y=exp(-(x-5).^2);ipeak([x' y'],1)
T. C. O'Haver (,

Cite As

Tom O'Haver (2021). iPeak (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (36)

amir afrashtehpour

Tom O'Haver

Note to Amir and others. If you want a plain function version of iPeak that does not plot or print anything, use one the the plain functions in this distribution: findpeaksG or findpeaksL or findpeaksfit or findpeaksb or findpeaksb3. All are described on iPeaks is strictly a keypress-operated interactive version.

Tom O'Haver


My understanding is that if an internal sub-function has the same name as an external function in the path, the internal function takes precedence. Is it the same for the Unix version?


Hi Tom,

I'm using 2015b (full uni version) now and I'm getting an error when using findpeaksb or findpeaksfit:
Error: File: findpeaksb.m Line: 1035 Column: 29
Function with duplicate name "val2ind" cannot be defined.

There is actually such a function in:
C:\Program Files\MATLAB\R2015b\toolbox\phased\phased

this might call that problem. Any tips?

amir afrashtehpour

this is really a great upload
but could you please make some adjustment for those of us who really need to use it as only a function
as i said i try to use this in a loop to only get the intensity location of the peak and etc,
i tired to delete all disp( title( plot( and etc
but for some reason it still logs numerals 1 continuously
could you provide a way to stop all the plotting and etc
which would do 2 things
stop interference with my gui
secondly as the loop calls this function 100,000 it sould really help with speed of things

Tom O'Haver

Amir, you could delete all the "plot(", "subplot(", "xlabel", "ylabel", and "title" statements in the code. Or you could use the findpeaksG.m function instead of ipeak.m, which does not plot but only returns the peak table.

amir afrashtehpour

thanks for the great upload
it really works great
i have a question
i want to run this program in a loop (the ipeak.m)
i have done that and it works great.
but i don't need the code to plot it i just want the "ans" data
how to modify the code to turn of plotting
is it possible for you to change the code so the ipeak.m take a function like yes or no as input argument to wether plot the data or not


what is the "width" this function gives for a gaussian fit i.e.? it is not n*sigma, so I'm at a loss ATM.

nevermind, I got it .... FWHM


Fantastic work! Thanks a lot!


Tom O'Haver

Oops, I forgot to include that function. I'll fix it, but in the meantime, here it is:

function [index,closestval]=val2ind(x,val)
% Returns the index and the value of the element of vector x that is closest to val
% If more than one element is equally close, returns vectors of indicies and values
% Tom O'Haver ( October 2006
% Examples: If x=[1 2 4 3 5 9 6 4 5 3 1], then val2ind(x,6)=7 and val2ind(x,5.1)=[5 9]
% [indices values]=val2ind(x,3.3) returns indices = [4 10] and values = [3 3]

aoli ao

Thanks so much for sharing this useful toolbox. I have a question here. when I tried to run ipeakdemo2.m or DemoFindPeaksb.m, it kept saying
??? Undefined function or method 'val2ind' for input arguments of type 'double'.

so how should do with this problem? Thanks for your help!

Tom O'Haver

iPeak version 5.9, submitted this morning, adds the percent fitting as the 6th column of the peak table. Thanks to helin for making this suggestion. Also available on


Thanks for your quick answer. I believe an estimate of fitting error would be extremely useful, so that we can have a quantified understanding of goodness of fit quickly. Thanks again.


Thanks for your quick answer. I believe an estimate of fitting error would be extremely useful, so that we can have a quantified understanding of goodness of fit. Thanks again.

Tom O'Haver

In iPeak, the fitting error is calculated only when you perform a curve fit (N or M keys). It's displayed in the x-axis label of the upper panel. You don't need to specify estimated/known peaks because it gets that info from the peak table.
But if you mean peak parameter error instead of fitting error, then you must use my ipf.m or peakfit.m programs instead, which include a bootstrap error estimate capability.
Would it be useful to add an estimate of fitting error to the usual peak parameter results?


Thanks for this great code. I want to learn if there is a way to calculate fitting error if we do not have any estimated/known peaks in contrast to 11-input case.

Tom O'Haver

Thanks, Bernhard, that's good advice. I'll look into "namespace" and "nlimt".

I do not believe "findpeaks" was a basic built-in function when I first created my function (back in Matlab 6.5), but I agree that I should change the name of my function to avoid the conflict.


Great and very useful work!
The only drawbacks are that the function findpeaks already comes with MATLAB and you make heavy use of global variables (can lead to bad side effects when used in conjunction with other code).
Consider using a namespace or rename the function and generally check mlint results (this should also improve performance).

Tom O'Haver

Thanks, Robert. After doing an iterative curve fit, the best-fit parameters are printed out in the command window. Or did you mean something else by "write the outcome"?


Hi Tom, thanks for sharing this wonderful code. It is not possible to write the outcome obtained after iterative curve fitting to a variable or am I missing something?

Tom O'Haver

Version 4 is now available from It adds the 'H' key that more easily detects 'shoulders' and poorly-resolved peaks.

Tom O'Haver

Kenric Lam, thanks for the heads-up. That bug has been tracked down and fixed in iPeak version 3.9, just uploaded to the file exchange and also available at

Kenric Lam

It is a big trap that there is a subfunction 'findpeaks' in 'ipeak.m' that is the same to the separate file 'findpeaks.m', but the results given are different.

Eric Diaz

Wonderfully documented!

Eric Diaz

Great code Tom!


wonderful code !!!

Tom O'Haver

Version 3 adds iterative curve fitting of the detected peaks with selectable peak shapes.

Tom O'Haver

Version 2.0, June 24, 2011, adds two main new features. You can set the initial values of pan and zoom in optional input arguments 7 ('xcenter') and 8 ('xrange'). There is also an optional "peak identification" function if optional input arguments 9 ('MaxError'), 10 ('Positions'), and 11 ('Names') are included. Type Help ipeak for more info.

Tom O'Haver

Version 1.4 fixed a problem with missing functions.

Tom O'Haver

Version 1.3 has improved input argument flexibility, accepts signals as separate x and y vectors or a two-column or two-row matrix. Adds a function to restore the original signal after the baseline has been subtracted. Adds peak area to the peak table.

Tom O'Haver

Version 1.2 (April 2011) has improved graphic performance on recent versions of Matlab.

Tom O'Haver

That should work, Aurora. Download the iPeak function, put it in the Matlab path, then type "help ipeak".


I want to use it to detect density peaks in my 1 dimensional data. How to do this? Thanks!

Andre Guy Tranquille

Tom O'Haver

Version 1.1 posted 25 Apr 2009 adds keypress command "L" which toggles ON and OFF peak labels in the upper panel (zoomed-in segment). Each peak in that segment is labeled with peak position, height, and width. The labels change in real-time as the peak detection parameters are modified.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!