Take the 2-minute tour ×

I have a sequence of data. So I want to plot that data inside the sliding windows due to windows length. Help me pls. Actually data is from mean and variance of frames. So I want to plot that mean and variance inside the sliding windows. Also I cant create sliding windows on matlab.

thank you

share|improve this question
is your signal 1D or 2D? –  natan Feb 15 '13 at 8:17
1D mean and variance is from image frame , So I plot one by one, –  Bayanaa Feb 15 '13 at 8:20
the term sliding window is misleading, instead the OP wants to only show\treat part of the data he\she already has, i.e. no calculation is needed. –  natan Feb 15 '13 at 9:00
add comment

3 Answers

So if I understand you correctly you want to change the x-axis limits of the plot. Use xlim for that, for example:

 xmin = 5;
 xmax = 7.6;
 xlim([xmin xmax])

or if you want a window of a constant size you can xlim([xmin xmin+window]) etc...

share|improve this answer
it does not make sense, –  Bayanaa Feb 15 '13 at 8:23
I think u got wrong my questions. I have mean and variance values. I just want to plot them inside the sliding windows –  Bayanaa Feb 15 '13 at 8:27
So I don't understand the question, what is a "sliding window", and why can't you just plot the data you have? can you add an example maybe? –  natan Feb 15 '13 at 8:28
postimage.org/image/io8q3kcm3 here is figure –  Bayanaa Feb 15 '13 at 8:31
So I want to plot only data which is inside the dashed windows –  Bayanaa Feb 15 '13 at 8:33
show 4 more comments

My approach would be,

a = randi(100,[1,50]);  % My sequence
win_width = 10;  %Sliding window width
slide_incr = 1;  %Slide for each iteration
numstps = (length(a)-win_width)/slide_incr; %Number of windows
for i = 1:numstps
   mean_win(i) = mean(a(i:i+win_width));  %Calculation for each window

there may be better ways of doing it..

share|improve this answer
FYI you can calculate the mean without a loop by just mean_win=conv(a,ones(1,win_width)/win_width), but that is not what the OP wanted (see comments of other answer) –  natan Feb 15 '13 at 9:16
For what I've checked, numstps should be added by one and win_width in the for loop should be diminished by one. Can someone confirm? –  Filippo Mazza Aug 28 '13 at 15:08
add comment

This is how I've always done it (adapted from code for 2 sliding windows). You can calculate the mean and variance however you'd like.

T = 25;                 % Window Size
K = size(data,1) - T;   % Number of repetitions

for i = 1:K    
   window = data(i:i+T-1,:);

   % Mean and Variance Calculations here
   % Plotting here
   % call 'drawnow' for incremental plotting (animation)
share|improve this answer
add comment

Your Answer


