Financial Time Series | ![]() ![]() |
Indexing a Financial Time Series Object
You can also index into the object as with any other MATLAB variable or structure. A financial time series object lets you use a date string, a cell array of date strings, a date string range, or normal integer indexing. You cannot, however, index into the object using serial dates. If you have serial dates, you must first use the MATLAB datestr
command to convert them into date strings.
When indexing by date string note that:
Indexing with Date Strings
With date string indexing you get the values in a financial time series object for a specific date using a date string as the index into the object. Similarly, if you want values for multiple dates in the object, you can put those date strings into a cell array and use the cell array as the index to the object. Here are some examples.
This example extracts all values for May 11, 1999 from myfts
.
format short myfts('05/11/99') ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series1: (1)' 'series2: (1)' '11-May-1999' [ 2.8108] [ 0.9323]
The next example extracts only series2
values for May 11, 1999 from myfts
.
myfts.series2('05/11/99') ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series2: (1)' '11-May-1999' [ 0.9323]
The third example extracts all values for three different dates.
myfts({'05/11/99', '05/21/99', '05/31/99'}) ans = desc: (none) freq: Unknown (0) 'dates: (3)' 'series1: (3)' 'series2: (3)' '11-May-1999' [ 2.8108] [ 0.9323] '21-May-1999' [ 0.9050] [ 1.2445] '31-May-1999' [ 1.4266] [ 0.6470]
The next extracts only series2
values for the same three dates.
myfts.series2({'05/11/99', '05/21/99', '05/31/99'}) ans = desc: (none) freq: Unknown (0) 'dates: (3)' 'series2: (3)' '11-May-1999' [ 0.9323] '21-May-1999' [ 1.2445] '31-May-1999' [ 0.6470]
Indexing with Date String Range
A financial time series is unique because it allows you to index the object using a date string range. A date string range consists of two date strings separated by two colons (::
). In MATLAB this separator is called the double-colon operator. An example of a MATLAB date string range is '05/11/99::05/31/99'
. The operator gives you all data points available between those dates, including the start and end dates.
Here are some date string range examples.
myfts ('05/11/99::05/15/99') ans = desc: (none) freq: Unknown (0) 'dates: (5)' 'series1: (5)' 'series2: (5)' '11-May-1999' [ 2.8108] [ 0.9323] '12-May-1999' [ 0.2454] [ 0.5608] '13-May-1999' [ 0.3568] [ 1.5989] '14-May-1999' [ 0.5255] [ 3.6682] '15-May-1999' [ 1.1862] [ 5.1284] myfts.series2('05/11/99::05/15/99') ans = desc: (none) freq: Unknown (0) 'dates: (5)' 'series2: (5)' '11-May-1999' [ 0.9323] '12-May-1999' [ 0.5608] '13-May-1999' [ 1.5989] '14-May-1999' [ 3.6682] '15-May-1999' [ 5.1284]
As with any other MATLAB variable or structure, you can assign the output to another object variable.
nfts = myfts.series2('05/11/99::05/20/99');
nfts
is the same as ans
in the second example.
If one of the dates does not exist in the object, an error message indicates that one or both date indexes are out of the range of the available dates in the object. You can either display the contents of the object or use the command ftsbound
to determine the first and last dates in the object.
Indexing with Integers
Integer indexing is the normal form of indexing in MATLAB. Indexing starts at 1 (not 0); index = 1 corresponds to the first element, index = 2 to the second element, index = 3 to the third element, and so on. Here are some examples with and without data series reference.
Get the first item in series2
.
myfts.series2(1) ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series2: (1)' '11-May-1999' [ 0.9323]
Get the first, third, and fifth items in series2
.
myfts.series2([1, 3, 5]) ans = desc: (none) freq: Unknown (0) 'dates: (3)' 'series2: (3)' '11-May-1999' [ 0.9323] '13-May-1999' [ 1.5989] '15-May-1999' [ 5.1284]
Get items 16 through 20 in series2
.
myfts.series2(16:20) ans = desc: (none) freq: Unknown (0) 'dates: (5)' 'series2: (5)' '26-May-1999' [ 0.2105] '27-May-1999' [ 1.8916] '28-May-1999' [ 0.6673] '29-May-1999' [ 0.6681] '30-May-1999' [ 1.0877]
Get items 16 through 20 in the financial time series object myfts
.
myfts(16:20) ans = desc: (none) freq: Unknown (0) 'dates: (5)' 'series1: (5)' 'series2: (5)' '26-May-1999' [ 0.7571] [ 0.2105] '27-May-1999' [ 1.2425] [ 1.8916] '28-May-1999' [ 1.8790] [ 0.6673] '29-May-1999' [ 0.5778] [ 0.6681] '30-May-1999' [ 1.2581] [ 1.0877]
myfts(end) ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series1: (1)' 'series2: (1)' '19-Aug-1999' [ 1.4692] [ 3.4238]
The last example uses the MATLAB special variable end
, which points to the last element of the object when used as an index. The example returns an object whose contents are the values in the object myfts
on the last date entry.
![]() | Object to Matrix Conversion | Operations | ![]() |