MATLAB Answers

Plotting Multiple Data Streams with Timestamp

9 views (last 30 days)
CW on 17 Nov 2020
Commented: Peter Perkins on 19 Nov 2020
Hi everyone,
I am struggling to vertically concatenate data streams from 3 devices on to a singular timestamped plot. The data outputs do not have the same number of columns. (I have simulated this effect by populating with zeros.) Initially, my workaround was to copy and paste the data into a singular file to create a stackedplot from a time table with smaller datasets.
I have attached my code and some example data. See Data2 in comment below.
Is there a better way to plot multiple data streams with the same x-axis timestamp than manually concatenating the data using timetable and stackedplot?
Any advice would be much appreciated. Many thanks for considering my request!


Show 2 older comments
CW on 18 Nov 2020
Hi dpb,
Thank your for your feedback. I have attached an example image of the desired plot. I am using three devices with different sampling frequencies, and I need a way to compare the output of those streams vs. a singular time-stamped axis. I was able to achieve the plot by creating a manually concatenated file of the three data streams in Excel, since the arrays are different sizes both vertically and horizontally, but this is not feasible for larger datasets. (Maximum number of excel columns.)
For now, my current solution for larger data sets is to use zeros(), vertcat(), and horzcat() to concatenate the data from the three devices and retain the appropriate time stamps. Below is the code used to plot the data (two streams from two devices only):
% Time = vertcat timestamp columns from Data1 & Data2
% zeros to make data streams Data1 & Data2 same size
% vertcat Data1 & Data2
Data = horzcat(Time,Data1,Data2);
t = table2timetable(Data);
s = stackedplot(t);
This works okay, but the result is a ton of zeros in the plot as well from forcing the concatenation into a singular array/table.
Please let me know if this clarifies my request, and thanks in advance!
dpb on 19 Nov 2020
Let's start with the basics -- how do you get the data from each device and what do you have that identifies what is in each?
Why can't you just read each file and plot what's in it and then go on to the next?
Is this in real time or post-processing data collected first?
CW on 19 Nov 2020
Hi dpb,
Thank you so much for your continued feedback and assistance. The short version is that there is no practical way to synchronize the data streams from the device on the front end during data collection.
I have created stackedplots from timetables for the three .csv files output from the three devices, but generating a plot for all the data streams vs. the time stamp quickly during post-processing is invaluable for this application. Please see my response to Peter Perkins below for more details. I hope to post my solution within the next week.
Thanks again!

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 19 Nov 2020
CW, if I am undersatand correctly, you are looking for the same kind of chart that stackedplot makes, for multi-rate data. The only way to get a stackedplot currently is to have one timetable. You'd want to horzcat your three different timetables, but you have multi-rate data, so the only way to "horzcat" them is to use synchronize to upsample or downsample to one common time vector.
It's hard to say from your figure if resampling would mess you up or not. It would be useful to hear if it would, and why. The obvious reason is that it would be either adding data that you don't really have, or removing data that you do have.


CW on 19 Nov 2020
Thank you for your feedback, Peter! Your explanation confirms my understanding that only a single time table can be used for stackedplot. Thus, my current solution to force the three data streams into a single timetable via upsampling seems to be the best solution while still maintaining the original sampling rates. From what I can tell with the sample data, upsampling and plotting via "scatter" satisfies my needs. As much as I would love to downsample or solely have plots of each device's data streams, the "all data streams vs. timestamp" figure is a great tool to quickly relate positional data from an accelerometer to other data streams for post-processing. Once I get some decent data, I will be happy to share the resultant plot and code to this thread to assist others in the future. Thanks again!
Peter Perkins
Peter Perkins on 19 Nov 2020
Well hang on: I was perhaps responding to a question that you didn't actually ask, "how do I use stackedplot with timetables that have different sampling rates?"
If you want to make one scatter plot with the three sources overlayed, there's no reason why you can't use hold on to do that. And also, there's no reason why you can't make something like stackedplot by using subplots and setting the axis limits the same on each. Something like either one of these might fit your needs:
tt1 = timetable((1:100)'/100,'StartTime',seconds(0),'SampleRate',100);
tt2 = timetable((1001:2000)'/1000,'StartTime',seconds(0),'SampleRate',1000);
hold on
hold off
xlim(seconds([0 1]));
xlim(seconds([0 1]));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!