MATLAB Answers

How to go about grouping, listing and (if user specifies) writing to a file a group of time series objects which are of different lengths (# of rows) and different time bases?

5 views (last 30 days)
Paul
Paul on 15 Mar 2018
Commented: Paul on 22 Mar 2018
I am trying to group the various time series objects into a single data structure ( currently I'm doing this with a cell data structure). But I need to have a way to see the whole data listed when I double click on the cell from the Workspace. Also I need to have a way to have this data printed if the user specifies. Additionally, I need to have a single time column being the first column of the cell and have the data shown for the correct times at which there's data available (e.g. tsObj1 may have data at t1 but tsObj2 may not have data at t1 until perhaps t3 but the cell would show times t1, t2, t3 with tsObj1 column having data at t1 and t2 having blanks (or NaN) for times t1 and t2).
In summary, Want to have
t tsObj1 tsObj2
-- ------------ -------------
t1 datatsObj1_1 NaN
t2 datatsObj1_2 NaN
t3 datatsObj1_3 datatsObj2_1
I really would like to use timetable() and/or tscollection() but I am unable to use these things because my data have different number of rows. (How do I go about making all of my timeseries objects have the same number of rows?).
Thank you for your help, effort, advice, time.

  0 Comments

Sign in to comment.

Accepted Answer

Eric Sofen
Eric Sofen on 20 Mar 2018
Take a look at the synchronize function for timetables. By default, synchronize(tt1,tt2) will construct a new time table with a time vector that is the union of the time vectors of tt1 and tt2. If there is data for a particular time in tt1 but not tt2 (or vice versa), which is what leads to the timetables being different lengths, it will fill with a missing value (e.g. NaN).

  3 Comments

Paul
Paul on 20 Mar 2018
This is what I want Eric. I tried doing:
tmpTT = timetable(m1, m2);
and received the following error:
Error using timetable (line 202)
Provide datetime or duration vector for row times to
create timetable.
When I try to specify a time (which I thought I didn't want to do since I want the function to compute the union of the two time series object times) tmpTT = timetable(m1.Time, m1,m2) :
Error using tabular/countVarInputs (line 227)
All variables must have the same number of rows.
Error in timetable (line 158)
[numVars,numRows] =
tabular.countVarInputs(varargin);
My time series time units are in seconds.
>> m1.TimeInfo.Units
ans =
seconds
>> m2.TimeInfo.Units
ans =
seconds
I proceeded to take an equal size subsample from both time series and rerun the commands and I'm still getting errors. Please see the following:
>> m1short = getsamples(m1, 1:20);
>> m2short = getsamples(m2, 1:20);
>> tmpTT = timetable(m1short,m2short);
Error using timetable (line 202)
Provide datetime or duration vector for row times to
create timetable.
>> tmpTT = timetable(m1short.Time,m1short,m2short);
Error using tabular/countVarInputs (line 227)
All variables must have the same number of rows.
Error in timetable (line 158)
[numVars,numRows] =
tabular.countVarInputs(varargin);
>> m1short.Length
ans =
20
>> m2short.Length
ans =
20
>> size(m1short.Time)
ans =
20 1
Ultimately, I'd like to have something like the following being displayed in Matlab's Variable Window once I double click on the variable from the Workspace and also be able to print this table to a file that I can view :
t m1 m2
- ----- ------
t1 m1_d1 NaN
t2 NaN m2_d1
t3 m1_d2 m2_d2
t4 NaN m2_d3
Thank you Eric.
Eric Sofen
Eric Sofen on 20 Mar 2018
One issue you're running into is that the time vector in a timetable must be a datetime or duration datatype, but in timeseries it's stored as a double. Also, make sure that both the data and time are column vectors. To convert your timeseries to timetable, you need to pull out the vectors from the timeseries and pass them into the timetable constructor. Something like this:
tt1 = timetable(seconds(m1.Time), m1.Data(:))
tt2 = timetable(seconds(m2.Time), m2.Data(:))
tt = synchronize(tt1,tt2)
Paul
Paul on 22 Mar 2018
Thank you very much Eric. This is the approach that worked for me. It really works well. Thank you for your time and effort!

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!

Translated by