View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0 | 6 ratings Rate this file 22 Downloads (last 30 days) File Size: 6.09 KB File ID: #12550 Version: 1.1



Jos (10584) (view profile)


05 Oct 2006 (Updated )

apply function to a moving window over a vector (v4.1, feb 2015)

| Watch this File

File Information

R = SLIDEFUN(FUN, W, V) evaluates the function FUN to a moving window of W consecutive elements of the vector V.
The function FUN is specified by a function handle or a function name and should return a scalar for a vector input. W specifies the size of the window and should be a positive integer. At the two edges of the vector less than W elements will be used. R will have the same size as V.

Effectively SLIDEFUN applies the function FUN to a moving window of consecutive elemens V(x0:(x0+W)) using FEVAL, and returns the result in R(x).

The window [x0:(x0+W)] is relative to the current point x in V. R = SLIDEFUN(FUN, W, V, WINDOWMODE) denotes the type of windowing being used: central (default), backward and forward.
R = SLIDEFUN(FUN,W,V,WINDOWMODE, P1,P2,...) provides for aditional parameters which are passed to the function FUN.
Example 1) Sliding max filter - return the maximum of every three consecutive elements:
  V = [1 2 3 9 4 2 1 1 5 6] ;
  R = slidefun(@max, 3, V)
  % -> [2 3 9 9 9 4 2 5 6 6]
  % So R(i) = max(R(i-1:i+1)) ;
  % and R(1) = max(V(1:2))

More examples are provided in the help section.


Movingstd & Movingstd2 inspired this file.

This file inspired Sound Pressure Level Calculator.

MATLAB release MATLAB 7 (R14)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
14 Oct 2016 K E

K E (view profile)

In R2016a and on, there are faster built in functions like movmin, movmax, movstd. See here,

Comment only
05 Aug 2015 K E

K E (view profile)

Very useful. I use this all the time. Wish it was part of Matlab. Also wonder if there is a way for it to run faster for long time series.

06 Nov 2014 Durga Lal Shrestha

It would be good if this function checks that input is vector. Otherwise for matrix it produces erroneous values.

Comment only
05 May 2014 Chad Greene

Chad Greene (view profile)

Works perfectly; thanks for sharing.

21 Jun 2012 Jos (10584)

Jos (10584) (view profile)

There is no stdev function in matlab. Do you mean STD?


Comment only
18 Jun 2012 Mar

Mar (view profile)

I can't get this to work for stdev.
NgridSD = slidefun(@stdev,25,Ngrid(4,:));
??? Error using ==> feval
Undefined function or method 'stdev' for input arguments of type 'double'.

Any ideas? Thank you.

Comment only
16 Nov 2010 Peter

Peter (view profile)

Very helpful for technical analysis of stock data. Thank you good sir.

26 Sep 2008 Carlos Adrian Vargas Aguilera

I just found I haven't rate it. Well done, and waiting for annonymous function for inputs... :)


02 Apr 2008 Dan Haeg

Very useful code for me, thanks for plugging it on CSSM so I could find it.

22 Jan 2007 John D'Errico

Nice, well written utility. It allows you to apply any general function on a moving window. Good help, examples, error checks, well done.

04 Feb 2015 1.1

fixed narginchk warning, treat V always as a vector even when it is a matrix

Contact us