How to determine if matrix elements are incremented on a regular interval?

Hello, I have a matrix of timestamps that an event occurs. I am trying to determine where the event occurs for over 6 seconds without interruption, and then storing the starting time stamp and the duration of the un-interrupted interval. The data was collected at 5Hz so to determine if the data is continuous I compare if the next element in the array is equal to the value of the previous element + 0.2 sec and keeping track of how many elements are continuous while clearing the counter when there is a break. It seems that when my script has to compare a whole number the comparision does not evaluate to 'true' if the next element is 0.2 seconds greater than the previous. I have verified all elements in my input data are the same type and have tried other methods without success. Any guidance would be appreciated.
Below is my code and attached is my input data.
%timeStamp = load(timeStamp.mat); Unsure how to load attached file
Unable to resolve the name 'timeStamp.mat'.
info = {'Time Start','Duration'};
rowNum = 2;
c = 0;
for n = 1:length(timeStamp) % Loop through every timestamp where event occurs
for m = n:length(timeStamp)-1 % Loop to find length of time event occurs
if timeStamp(m) + 0.2 == timeStamp(m+1)
c = c + 1; % Count consecutive time stamps
else
if c > 30 % if fuel is cut for more than 6 sec store in cell array
info{rowNum,1} = timeStamp(n);
info{rowNum,2} = (c+1) .* 0.2;
rowNum = rowNum + 1;
end
c = 0; % reset counter to find where other >6sec event occurances are
break
end
end
end

 Accepted Answer

"timeStamp(m) + 0.2 == timeStamp(m+1)"
You should never compare floating points with "=="
rather
threshold = 1e-10;
if abs(timeStamp(m) + 0.2 - timeStamp(m+1)) < threshold
...
end

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!