How do I extract data from a matrix based on gaps in time?

* I am still trying to figure this out. Any other suggestions? *
I am analyzing engine data and need to extract multiple matrices from one large matrix based on gaps in time.
I began with a matrix of 500,000 rows and 5 columns. I was able to detect when the electric assist came on in the engine (when the values in column 2 == 1). Now I am left with a matrix of 160,000 data points. I need to separate the different instances of when the electric assist was on, so I can look at the data in columns 3, 4, and 5 for each instance. There will be a gap in time (column 1) when the data moves from one instance of "eAssist" to another.
How do I detect that time gap and then create submatrices that represent each instance?
Here is my code so far:
filename = 'Ferrari_Main.xlsx'; % Specifies the excel file
Data = 'Sheet1'; % Specifies the sheet in the file
Time = 'A:A'; % Labels column A
eAssist = 'B:B'; % Labels column B
EngSpeed = 'C:C'; % Labels column C
EngTorq = 'D:D'; % Labels column D
TMOT = 'E:E'; % Labels column E
TimeD = xlsread(filename, Data, Time); % Extracts time data
eAssistD = xlsread(filename,Data,eAssist); % Extracts eAssist data
EngSpeedD = xlsread(filename,Data,EngSpeed); % Extracts engine speed data
EngTorqD = xlsread(filename,Data,EngTorq); % Extracts engine torque data
TMOTD = xlsread(filename,Data,TMOT); % Extracts TMOT data
% Find when eAssist is ON
Dat = [TimeD eAssistD EngSpeedD EngTorqD TMOTD];
ind1 = Dat(:,2) == 1; % Find all eAssist values of 1
eA = Dat(ind1,:); % Data with eAssist ON
Now I need to find when there is a gap in the time series in column 1 and create a matrix with all of the data within the continuous time series before the gap.

2 Comments

I am still trying to figure this out. Any other suggestions?
You've been given two answers, one you don't even appear to have looked at, the other you've accepted. What more do you want?
After 9 days, these are probably the only 3 suggestions you'll get. Otherwise you'd probably have gotten them by now.

Sign in to comment.

 Accepted Answer

You can convert the regions of 1's in the eAssist column (col 2) to separate, labeled regions/groups with bwlabel() if you have the Image Processing Toolbox. Type ver to find out. This simply assigns a label (like 1,2,3,4,5,...) to each group of 1's. Do this:
[labeledGroups, numGroups] = bwlabel(eAssist);
Now you can process each group by itself, like in a for loop over all groups or something.
for k = 1 : numGroups
% Find out what rows are in this group.
indexesInThisGroup = find(labeledGroups == k);
% Extract only those rows and do something with them.
end

2 Comments

Thanks! I am using R2015b and do not have access to the Image Processing Toolbox. Do you have a suggestion for a way to do this without that toolbox?
You could make your own labeledGroups by simply running down the column vector and each time you encounter a 1, when you had been on a 0, increment the label index. Then when you encounter a zero again, stop labeling with that index and start labeling with 0.
eAssist = [1;1;1;0;1;1;0;1;1;1;0;1] % Sample data
% Initialize labels and arrays.
labeledGroups = zeros(size(eAssist));
currentLabel = 0;
lastLabel = 0;
% Label each group of 1's
for row = 1 : length(eAssist)
if eAssist(row) == 1
% We're in a group of 1's.
if currentLabel == 0
% If we just came from outside a group, increment the label number.
lastLabel = lastLabel + 1;
end
% Assign this label number to this index.
labeledGroups(row) = lastLabel;
currentLabel = lastLabel;
else
% eAssist = 0
currentLabel = 0;
end
end
labeledGroups

Sign in to comment.

More Answers (1)

First, a much simpler way to read your data:
filename = 'Ferrari_Main.xlsx'; % Specifies the excel file
enginedata = readtable(filename, 'Sheet', 'Sheet1', 'Range', 'A:E', 'VariableNames', ...
{'Time', 'eAssistD', 'EngSpeedD', 'EngTorqD', 'TMOT'});
That's it. And most likely you don't even need the 'Sheet', 'Sheet1', 'Range', 'A:E' part.
From that, or your original variables it's easy to find your groups. A group starts when the difference between two consecutive eAssistD values is 1, it ends when that difference is -1. The difference is always 0 otherwise. Therefore
transitions = find(diff([0; enginedata.eAssitD; 0]); %the 0 padding is to make sure that 1 at the start or end of the sequence are found
%odd indices in transitions are start of sequences. even indices are 1 past the end of the sequences
startsequences = transitions(1:2:end);
endsequences = transitions(2:2:end) - 1;
From there you can do whatever you want, e.g. assign a label to each sequence:
labels = zeros(height(enginedata), 1);
for seq = 1:numel(startsequences)
labels(startsequences(seq):endsequences(seq)) = seq;
end
enginedata.Labels = labels;
And then calculate the mean speed for each sequence:
meanspeed = rowfun(@mean, enginedata, 'InputVariables', 'EngSpeedD', 'GroupingVariables', 'Labels', 'OutputVariableNames', 'meanSpeed');

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!