MATLAB: Help with breaking into windows based on a condition?

conditionloops

Hi, I have a array of numbers (0,1) which is attached in the CSV file. I want to break them into windows where the condition meets(1) for greater than or equal to 30 seconds.
If it is for only 10 seconds continuous it shouldn't be considered a window. I also want the locations for the start and stop of the windows.

Best Answer

Assuming the spacing between elements if 1 second, you can use regionprops() to measure the starting and stopping indexes of each region:
% Read data file.
data = fileread('nte.csv');
% Skip 'condition' and convert from characters to numbers.
data = sscanf(data(10:end), '%d')
% Get rid of regions fewer than 30 elements
data = bwareaopen(data, 30);
% Assign an ID number to each contiguous region of 1's.
[labeledRegions, numRegions] = bwlabel(data);
% Find starting and stopping indexes, plus area, for each region.
props = regionprops(labeledRegions, 'PixelList', 'Area');
% props is a structure array.
% Get all the areas into single arrays for convenience.
allAreas = [props.Area]
for k = 1 : numRegions
starts(k) = props(k).PixelList(1, 2);
stops(k) = props(k).PixelList(end, 2);
end