Say I have a 2000 x 4 matrix, where column 1 is a time stamp, starting at 0 and going up to 200 in steps of 0.1. In the other columns (2-4) is data for different variables. What I'm looking to do is extract data from the matrix when one variable (column 3) is over a certain threshold, I also want to extract the previous and next 100 samples from when the value goes back below the threshold again. Here's my code so far:
X = rand(100,3);
T = [0.1:0.1:10]';
Data = [T, X];
overThresh = X(:,3) > 0.8;
SE = strel('arbitrary', ones(100,1));
extractVector=imdilate(overThresh,SE);
Event = Data(extractVector,:);
for i = 2:length(Event)
if Event(i,1) > 2 * Event(i-1,1)
Event1 = Low_Event(1:i-1,:);
Event2 = Low_Event(i:end,:);
end
end
From the code above I'm able to collect all the events that occur and even able to separate if there is 2 events occurring. However how would I be able to separate if there is more than 2 events occurring? Plus how would I be able to name them dynamically, such as Event 1, Event 2, Event 3 etc.
The steps I'm looking my code to carry out is: 1. Detect the violation of a threshold (called an event) 2. Extract 100 samples before and after event (the end of the event being when the samples drop below the threshold again) 3. Store individual event, Event 1...
I think the best way to do this is to use the time stamp, and this is what I've done in my above piece of code. Any help would be greatly appreciated.
Edit: Ok so I done some more work on this and my code now looks like this. I'm now able to extract the 1st event. However I need to restart the for loop, to make it run again for the new Event array I created (taking Event1 away from event). Here's my code now:
X = rand(100,3);
T = [0.1:0.1:10]';
Data = [T, X];
overThresh = X(:,3) > 0.8;
SE = strel('arbitrary', ones(100,1));
extractVector=imdilate(overThresh,SE);
Event = Data(extractVector,:);
cc = 1;
x = 0;
while ~x
x = 1;
for m = 2:length(Event)
if length(Event) > 0
if Event(m,1) > 2 * Event(m-1,1)
genvarname('Event', num2str(cc));
eval(['Event', num2str(cc) ' = Event(1:m-1,:);']);
Event = Event(m:end,:);
x = 0;
break
end
end
end
end
If anybody know a way to get the for loop to run again for the new Event variable please let me know