Time series data events

6 views (last 30 days)
Ben Gibbingwork
Ben Gibbingwork on 4 Mar 2021
Commented: Ben Gibbingwork on 29 Mar 2021
Hola file exchange,
I have set of time series fiels for 6 locations. It is acceleration data from 6 sesnors deployed on machines. I also have a list of events in excel, in the structure in the image below. I want to mark the event on the time series plot, possibly with a line. How to plot the time series for each sensor and then only plot the events relating to that location - example - location number 1 has 2 events in the table below. So the time series plot should show these events.
Gracias todos

Accepted Answer

Peter Perkins
Peter Perkins on 5 Mar 2021
Edited: Peter Perkins on 5 Mar 2021
Ben, I'm not sure where you are starting from. I'm gonna show how to do this with timetables. Imagine you have a timetable containing data from three channels, sampled at 10Hz over 10 sec:
>> T = 10;
>> n = 100;
>> numChannels = 3;
>> X = cumsum(randn(n,numChannels));
>> tt = sortrows(array2timetable(X,"SampleRate",n/T));
tt =
8×3 timetable
Time X1 X2 X3
_______ ________ ________ _______
0 sec -0.33824 1.0395 0.94041
0.1 sec 0.19002 0.92269 1.2896
0.2 sec 1.2469 0.27439 3.1488
0.3 sec 1.2617 0.23641 4.0759
0.4 sec 2.8764 0.037084 2.849
0.5 sec 2.0606 0.91875 2.5217
0.6 sec 0.062812 0.86261 3.4134
0.7 sec -0.68538 0.021371 3.7015
[snip]
And imagine you have 8 events for those channels over that time range:
>> numEvents = 8;
>> events = timetable(seconds(sort(T*rand(numEvents,1))),randi(numChannels,numEvents,1),'VariableNames',"Channel")
events =
8×1 timetable
Time Channel
__________ _______
2.1327 sec 2
2.6871 sec 1
3.8015 sec 2
4.1693 sec 2
4.7535 sec 3
6.809 sec 2
7.7221 sec 3
9.8668 sec 2
The following code interpolates each channel at its event times and overlays that on a plot of the raw data:
for chnl = 1:numChannels
chnlEvents = retime(tt(:,chnl),events.Time(events.Channel == chnl),"linear");
subplot(3,1,chnl), plot(tt.Time,tt{:,chnl},'b-',chnlEvents.Time,chnlEvents{:,1},'r*','MarkerSize',10)
end
and results in a plot like this
The stackedplot function would be a fine way to plot the raw data on three axes, but that will not let you add the overlay. So the code above makes the three plots "by hand".
  2 Comments
Adam Danz
Adam Danz on 5 Mar 2021
I agree with @Peter Perkins that this sounds like a job for stackedplot.
To add the vertical reference lines, you just need to specify the axis handles which can be done relatively easily using undocumented methods explained here in this answer.
Ben Gibbingwork
Ben Gibbingwork on 29 Mar 2021
Thank you for your help!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!