timeseries.setabstime
timeseries: ts = setabstime (ts, dates)
timeseries: ts = setabstime (ts, dates, format)
Set the time vector of a series from dates.
ts = setabstime (ts, dates) gives the samples
of the series ts the dates dates, one per sample, in order:
a cell array of character vectors, a string array, a character matrix
of one date per row, a character vector for a single sample, or a
datetime array. The earliest date becomes
TimeInfo.StartDate, stored as 'dd-mmm-yyyy HH:MM:SS'
with milliseconds as '.FFF' when the seconds are not whole, and
Time holds each date’s offset from it in the series’ own
TimeInfo.Units, which are kept: a series in seconds given one
date a day gets times [0 86400 172800 …]. Each date is read
by its own format. Dates given as times of day alone, such as
'13:00:00', give times since midnight and no start date, as in
MATLAB.
ts = setabstime (ts, dates, format) reads
every date strictly in the datestr format format. A date
not written in format is refused. The start date is stored as
above whatever the format, where MATLAB writes it in format, from
which every later reading has to guess it again: 'dd/mm/yyyy'
then comes back as a date of another month.
The dates need not be sorted: the samples, with their quality codes, are sorted with them, keeping the order of samples at equal dates, as the constructor sorts them.
MATLAB differs in four ways. It does not move the samples with unsorted
dates, so each is given another sample’s date. It reads every date by
the format of the first, so {'01-Jan-2024', '2024-01-02'} gives
a date in year 7. It forces text into format when it does not
fit, so 'yyyy-mm-dd' on '01-Jan-2024' gives a date in
year 6. And it takes datenum values, datetime values and
plain numbers as relative seconds, dropping the dates; here numbers are
refused, since a datenum cannot be told from a plain number, and
a datetime is read as its dates. Convert a datenum
x with datetime (x, 'ConvertFrom', 'datenum').
See also: timeseries.getabstime, timeseries.setuniformtime
Source Code: timeseries
setabstime gives the samples dates, keeping the series' own units, so a series in seconds given a date a day gets steps of 86400.
ts = timeseries ([10; 20; 30]);
ts = setabstime (ts, {'01-Mar-2024', '02-Mar-2024', '03-Mar-2024'});
ts.Time'
ans =
0 86400 172800
ts.TimeInfo.StartDate
ans = 01-Mar-2024 00:00:00
The dates need not be sorted: each sample keeps the date it was given, and the series is sorted with them. MATLAB sorts the dates alone, leaving every sample on another's date.
ts = timeseries ([10; 20; 30], [0; 1; 2], [1; 2; 3]);
ts.TimeInfo.Units = 'days';
ts = setabstime (ts, {'03-Mar-2024', '01-Mar-2024', '02-Mar-2024'});
getabstime (ts)
ans =
3x1 cell array
{'01-Mar-2024 00:00:00'}
{'02-Mar-2024 00:00:00'}
{'03-Mar-2024 00:00:00'}
[ts.Data, ts.Quality]
ans = 20 2 30 3 10 1