timeseries

Methods

Method Reference: timeseries.timeseries

timeseries: ts = timeseries ()

timeseries: ts = timeseries (name)

timeseries: ts = timeseries (data)

timeseries: ts = timeseries (data, time)

timeseries: ts = timeseries (data, time, quality)

timeseries: ts = timeseries (…, 'Name', name)

Create a timeseries object.

ts = timeseries () returns an empty series with no name.

ts = timeseries (name) returns an empty series named name, a character vector or a string scalar. MATLAB takes a string scalar here as the data of a one-sample series.

ts = timeseries (data) returns a series of the samples in data, a numeric or logical array, at times 0, 1, 2, … seconds. For data of two dimensions or fewer each row is a sample; for data of three dimensions or more each slice along the last dimension is one. A row vector of more than one element is taken as that many scalar samples and stored as a 1x1xN array.

ts = timeseries (data, time) takes the samples at time, which is one of:

  • a numeric vector of one time per sample, in seconds;
  • a cell array of character vectors, a string array or a datetime array of dates, one per sample, which gives Time in days from the earliest of them, with that date as TimeInfo.StartDate; each date is read by its own format, a time zone is dropped and the clock time kept, and NaT is refused;
  • a duration array, relative times in the units its display format names: 's', 'm', 'h' and 'd' give seconds, minutes, hours and days, any other format seconds;
  • a cell array of numeric scalars, read as a numeric vector;
  • [], for the default times.

With N times, data of three dimensions or more must have N slices along its last dimension. A matrix with N rows has a sample per row; otherwise one with N columns is stored as an Rx1xN array, a sample per column, so a row vector of N elements gives N scalar samples; and with a single time the whole matrix is one sample. The times need not be sorted: the samples are sorted with them, keeping the order of samples at equal times. They are stored as double whatever their class. MATLAB accepts neither datetime nor duration arrays here.

ts = timeseries (data, time, quality) also sets a quality code per sample: integers from -128 to 127, as a vector of one per sample or an array the size of data as stored, or [] for none. A matrix stored a sample per column, as an Rx1xN array, takes codes per element in that shape too; MATLAB refuses them in the matrix’s own RxN shape, as here.

ts = timeseries (…, 'Name', name) names the series; the option name is matched in any case, and a later occurrence overrides an earlier one. A name that is not text is refused, where MATLAB ignores it.

ts = timeseries (ts0) returns the series ts0 itself.

See also: timetable, istimeseries, tsdata.timemetadata

Source Code: timeseries

A series is a sequence of samples, each taken at a time. The data alone gives the times 0, 1, 2, ... seconds, and the display summarises the series rather than listing it.

 ts = timeseries ([12.1; 14.5; 13.2; 15.8], 'Name', 'temp')
ts =

  timeseries

  Common Properties:
            Name: 'temp'
            Time: [4x1 double]
        TimeInfo: [1x1 tsdata.timemetadata]
            Data: [4x1 double]
        DataInfo: [1x1 tsdata.datametadata]
 [ts.Time, ts.Data]
ans =

         0   12.1000
    1.0000   14.5000
    2.0000   13.2000
    3.0000   15.8000

Times may be given, in any order: the samples are sorted with them. A third argument sets a quality code for each sample.

 ts = timeseries ([12.1; 14.5; 13.2], [30; 0; 60], [0; 1; 0]);
 [ts.Time, ts.Data, ts.Quality]
ans =

         0   14.5000    1.0000
   30.0000   12.1000         0
   60.0000   13.2000         0

Dates as text, or a datetime array, give times in days counted from the earliest date, which becomes TimeInfo.StartDate.

 ts = timeseries ([3; 5; 4], {'01-Mar-2024', '03-Mar-2024', '02-Mar-2024'});
 ts.Time
ans =

   0
   1
   2
 ts.TimeInfo.StartDate
ans = 01-Mar-2024 00:00:00
 ts.TimeInfo.Units
ans = days

A matrix holds one sample per row, and data of three dimensions one sample per slice along the last, which IsTimeFirst reports.

 ts2 = timeseries ([1, 10; 2, 20; 3, 30]);
 size (ts2.Data), ts2.IsTimeFirst
ans =

   3   2

ans = 1
 ts3 = timeseries (rand (2, 2, 5));
 size (ts3.Data), ts3.IsTimeFirst
ans =

   2   2   5

ans = 0