How to find end of time series in plot
    15 views (last 30 days)
  
       Show older comments
    
Hi!
This is kind of a basic question.
I have a file containing a number of keys/positions (x-pos, y-pox, etc..) that again contains data within arrays. There are some missing data in this file, and the missing data appears as a flat line in the plot. I only care for the missing data that last for more that 2 sec (2*mps). I have managed to find the index of the beginning of the missing data, I don't know how to find the end (or technically the whole missing data interval). The reason I want to find the end as well is that I want to plot figures that shows the interval of the missing data, and some time-buffer at both ends so I can see how the plot behaves right before the data goes missing, and right after. Is there some way to find the end of each interval (or the end of them)?
Thank you in advance! 
My code so far:
data = load('x.mat');
figure(1) 
plot(data.Time,data.PITCH);     %The whole graph
N = length(data.XPOS);
mps = length(data.Time)/(data.Time(end));       %measurements per second (about 28 meas/sec)
M = [];
arr = [];
counter = 0;
nc = 0;
nnc=0;
intervall=[];
index = [];
%len = [];
for i=1:length(data.XPOS)-1       
    if data.XPOS(i) == data.XPOS(i+1)       %if the data repeats itself
        counter = counter+1;
        if counter > mps*2    % if tangent is repeated for more than 2 sec
            nc = nc+1;
            arr(nc) = data.XPOS(i);
            %%%% trying to find end of interval here  
            if not(ismember(data.XPOS(i),M))
                nnc=nnc+1;
                M(nnc) = data.XPOS(i);      % the x-pos that have missing data for more than 2 sec
                intervall(nnc) = data.Time(i);      % time of the critically missed data
                index(nnc) = i;
            end
        end
    else
        counter = 0;
    end  
end
%%%%%%      Plot critical points        %%%%%%
start=[];
endd =[];
for k=1:length(M)
    start(k)= index(k)- 28*20;
    endd(k) =index(k) + 28*70;      %   +28*70 is buffer after the first index of missing data
    figure(k+1)    
    plot(data.Time(start(k):endd(k)),data.XPOS(start(k):endd(k)));     
end
4 Comments
Answers (1)
  Joseph Cheng
      
 on 23 Jun 2021
        to go with your example of pos where 
pos = [0.11, 0.12, 0.12, 0.12, 0.12 0.12, 0.14, 0.14, 0.11, 0.16, 0.13, 0.13, 0.13, 0.13];
you can find sequences of consecutive numbers by doing something like this
dpos = diff(pos); %diff from next
stalepos = [find(dpos~=0)+1 numel(pos)+1]%find edges of consecutive index where the difference is 0
for ind = 1:numel(stalepos)-1
   disp(pos(stalepos(ind):stalepos(ind+1)-1))  %display the values between the sequences.  
end
now that last for loop we can see which ones have length > your minimum not missing/stale data threshold you can do whatever you need to.
0 Comments
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

