MATLAB Answers

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

조회 수: 8(최근 30일)
Bill Tubbs
Bill Tubbs 2021년 4월 3일
댓글: Bill Tubbs 2021년 4월 5일
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
Bill Tubbs
Bill Tubbs 2021년 4월 4일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb 2021년 4월 4일
편집: dpb 2021년 4월 4일
"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).
  댓글 수: 8

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Bill Tubbs
Bill Tubbs 2021년 4월 4일
편집: Bill Tubbs 2021년 4월 4일
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:
  댓글 수: 4
dpb 2021년 4월 5일
" 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)...

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by