Get a subset of columns from a timeseries object as a timeseries

43 views (last 30 days)
Bill Tubbs
Bill Tubbs on 3 Apr 2021
Commented: Bill Tubbs on 5 Apr 2021
I have a timeseries containing data for 4 variables:
Ts = 0.2; nT = 50;
t = Ts*(0:nT-1)';
u1 = 50*idinput(nT);
u2 = 50*idinput(nT);
u3 = 50*idinput(nT);
u4 = 50*idinput(nT);
u = timeseries([u1 u2 u3 u4],t);
I see there is a function called getsamples to get a subset of the data samples.
E.g. to get the first data point:
>> getsamples(u,1)
Common Properties:
Name: 'unnamed'
Time: [1x1 double]
TimeInfo: [1x1 tsdata.timemetadata]
Data: [1x4 double]
DataInfo: [1x1 tsdata.datametadata]
More properties, Methods
But how do I get a subset of the columns as a timeseries?
u1 = ... % ?
E.g. what if I want to plot just u1?
Obviously, I can do this:
But isn't there a simpler way similar to how I would plot a timeseries with only one variable?
plot(u(:,1)) % indexing like this does not work
  1 Comment
Bill Tubbs
Bill Tubbs on 4 Apr 2021
Just noticed there is a new function called ts2timetable introduced in version 2021a which might be usefull for converting multiple timeseries objects into a timetable.

Sign in to comment.

Accepted Answer

dpb on 4 Apr 2021
Edited: dpb on 4 Apr 2021
"But isn't there a simpler way similar to how I would plot a timeseries with only one variable?"
Once you made a timeseries object out of a multi-dimensional array, no. The .Data property is a 2D array and so you must subscript the array to refer to only a single column (or subset of columns) just like any other array.
I've found the timeseries object to be more cumbersome than the features it offers owing to the way interaction with it is built; I'd probably just revert to a regular timetable instead, unless you do have the one or two specific things the timeseries supports like the event.
But, the object is constructed such that all the data associated with it is either a vector or a 2D array, it doesn't have the concept of there being separate but associated timeseries variables. That can only be done in a timetable
As noted, at least so far I've failed to find sufficient added features in the timeseries to be able to have found a use for it for either anything I've tried to do myself or even to answer any Q? posed here (other than just syntax issues like this one that (almost?) all of which have resulted in suggesting other solutions are more easily implemented than by continuing to fight the timeseries limitations/design).

Sign in to comment.

More Answers (1)

Bill Tubbs
Bill Tubbs on 4 Apr 2021
Edited: Bill Tubbs on 4 Apr 2021
Here is my answer to illustrates the timetables solution proposed by @dpb in the comments above.
u = timetable(seconds(t),u1,u2,u3,u4);
This does work in the sense that you can now easily select the data for one column:
or more than one column:
Timetables with one element are not supported by the plot function but you can use the stackedplot method:
Alternatively, you can get the desired column as an array like this:
dpb on 5 Apr 2021
" make plots with automatically-labelled axes and titles."
Oh. I don't know that I've ever utilized the feature "in anger" (as an old former colleague/Scottish power engineer was wont to say of beginning a test)...

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by