Choose a part of the time
In the code below, I am creating a simple data series, a time vector and then a time series. I have no problem plotting both of them. (It doesn't matter if they are on the same plot.) I have not been able to figure out how to multiply the timers part as shown by the last command, which fails with an error:
>> timeseriesTest
Error using timeseries/plot (line 27)
The plot method can only be used for a single timeseries object
Error in timeseriesTest (line 14)
plot(ts(25:end));
>>
How do I retrieve the last 25 (in this case) values ββin a time series? IMPORTANT: Although not the case in the code below, my time series have daily or weekly time stamps and I need to save that. I.e:.
NewData = ts.data
... is not a good answer, unless it is the only way to get new retrieved timers.
t=[1:50];
d=sin(2*pi*t/20);
ts = timeseries(2*d, t);
%plot data and timeseries
plot(d);
hold on;
plot(ts);
figure();
plot(d(25:end));
hold on;
plot(ts(25:end));
source to share
If you look at the properties of the object TimeSeries
when you run your code before trying and plotting, this is what we see:
>> ts
timeseries
Common Properties:
Name: 'unnamed'
Time: [50x1 double]
TimeInfo: [1x1 tsdata.timemetadata]
Data: [1x1x50 double]
DataInfo: [1x1 tsdata.datametadata]
You can see that your time series object has a field Data
as well as a field Time
that represents the time value in each point instance. If you want to individually access fields and display the last 25 items, follow these steps:
plot(ts.Time(end-24:end), squeeze(ts.Data(end-24:end)));
ts
is your object TimeSeries
, and if you want to access the fields inside that object, use the dot operator ( .
... and you already figured it out). When you use the dot operator, you simply access the field you want using its appropriate name. So if you want time values ββuse Time
, and if you want data use Data
. Now, what may seem strange, I used squeeze
. squeeze
removes the dimensions of the singleton. If you see Data
is an array 1 x 1 x 50
when it should just be an array 50 x 1
. The goal squeeze
is to remove excess sizes to get our actual data.
Note that this only happens if you only have one signal in your time series. If we have multiple signals ... let's say if we wanted three signals of length 50, we would create a 50 x 3 matrix where each columndenotes one signal. It will look something like this:
>> t = 1:50;
>> A = rand(50,3);
>> ts = timeseries(A,t)
timeseries
Common Properties:
Name: 'unnamed'
Time: [50x1 double]
TimeInfo: [1x1 tsdata.timemetadata]
Data: [50x3 double]
DataInfo: [1x1 tsdata.datametadata]
rand
generates a random matrix or vector of values ββof whatever size you want in the range [0-1]
. You will see that our signal is now 50 x 3
. If you want to plot this, plot
recognizes multiple signals over a period of time ... so you can simply do this:
plot(ts.Time, ts.Data);
This should generate a graph of three traces, each defined with a different color and within the same timeframe specified ts.Time
.
Likewise, if you want to plot the last 25 points for each signal, simply do:
plot(ts.Time(end-24:end), ts.Data(end-24:end,:));
This code will access the last 25 lines of every column (i.e. every signal) in yours Data
and plot them all.
source to share