How do I extract data from a matrix based on gaps in time?
Show older comments
* 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
Guillaume
on 16 Jun 2017
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?
Image Analyst
on 16 Jun 2017
After 9 days, these are probably the only 3 suggestions you'll get. Otherwise you'd probably have gotten them by now.
Accepted Answer
More Answers (1)
Guillaume
on 7 Jun 2017
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!