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 character vector, a
cell array of date character vectors, a date character vector 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 character vectors.
When indexing by date character vector, note that
Each date character vector must contain the day, month, and year. Valid formats are
'ddmmmyy hh:mm'
or'ddmmmyyyy hh:mm'
'mm/dd/yy hh:mm'
or'mm/dd/yyyy hh:mm'
'dd-mmm-yy hh:mm'
or'dd-mmm-yyyy hh:mm'
'mmm.dd,yy hh:mm'
or'mmm.dd,yyyy hh:mm'
All data falls at the end of the indicated time period, that is, weekly data falls on Fridays, monthly data falls on the end of each month, and so on, whenever the data has gone through a frequency conversion.
Indexing with Date Character Vectors
With date character vector indexing, you get the values in a financial time series object for a specific date using a date character vector as the index into the object. Similarly, if you want values for multiple dates in the object, you can put those date character vectors into a cell array of character vectors 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 myfts1('05/11/99')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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
:
myfts1.series2('05/11/99')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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:
myfts1({'05/11/99', '05/21/99', '05/31/99'})
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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 example extracts only series2
values for the same
three dates:
myfts1.series2({'05/11/99', '05/21/99', '05/31/99'})
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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 Character Vector Range
A financial time series is unique because it allows you to index into the object
using a date character vector range. A date character vector range consists of two
date character vector separated by two colons (::
). In
MATLAB this separator is called the double-colon operator. An example of a
MATLAB date character vector 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 character vector range examples:
myfts1 ('05/11/99::05/15/99')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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]
myfts1.series2('05/11/99::05/15/99')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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 = myfts1.series2('05/11/99::05/20/99');
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106)
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
:
myfts1.series2(1)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series2: (1)' '11-May-1999' [ 0.9323]
Get the first, third, and fifth items in series2
:
myfts1.series2([1, 3, 5])
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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
:
myfts1.series2(16:20)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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
:
myfts1(16:20)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) 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]
Get the last item in myfts1
:
myfts1(end)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/end (line 57) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) ans = desc: (none) freq: Unknown (0) 'dates: (1)' 'series1: (1)' 'series2: (1)' '19-Aug-1999' [ 1.4692] [ 3.4238]
This 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.
Indexing When Time-of-Day Data Is Present
Both integer and date character vector indexing are permitted when time-of-day
information is present in the financial time series object. You can index into the
object with both date and time specifications, but not with time of day alone. To
show how indexing works with time-of-day data present, create a financial time
series object called timeday
containing a time
specification:
dates = ['01-Jan-2001';'01-Jan-2001'; '02-Jan-2001'; ... '02-Jan-2001'; '03-Jan-2001';'03-Jan-2001']; times = ['11:00';'12:00';'11:00';'12:00';'11:00';'12:00']; dates_times = cellstr([dates, repmat(' ',size(dates,1),1),... times]); timeday = fints(dates_times,(1:6)',{'Data1'},1,'My first FINTS')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints (line 165) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) timeday = desc: My first FINTS freq: Daily (1) 'dates: (6)' 'times: (6)' 'Data1: (6)' '01-Jan-2001' '11:00' [ 1] ' " ' '12:00' [ 2] '02-Jan-2001' '11:00' [ 3] ' " ' '12:00' [ 4] '03-Jan-2001' '11:00' [ 5] ' " ' '12:00' [ 6]
Use integer indexing to extract the second and third data items from
timeday
:
timeday(2:3)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints (line 165) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) ans = desc: My first FINTS freq: Daily (1) 'dates: (2)' 'times: (2)' 'Data1: (2)' '01-Jan-2001' '12:00' [ 2] '02-Jan-2001' '11:00' [ 3]
For date character vector indexing, enclose the date and time character vectors in
one pair of quotation marks. If there is one date with multiple times, indexing with
only the date returns the data for all the times for that specific date. For
example, the command timeday('01-Jan-2001')
returns the data for
all times on January 1, 2001:
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints (line 165) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) ans = desc: My first FINTS freq: Daily (1) 'dates: (2)' 'times: (2)' 'Data1: (2)' '01-Jan-2001' '11:00' [ 1] ' " ' '12:00' [ 2]
You can also indicate a specific date and time:
timeday('01-Jan-2001 12:00')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints (line 165) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) ans = desc: My first FINTS freq: Daily (1) 'dates: (1)' 'times: (1)' 'Data1: (1)' '01-Jan-2001' '12:00' [ 2]
Use the double-colon operator ::
to specify a range of dates
and times:
timeday('01-Jan-2001 12:00::03-Jan-2001 11:00')
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints (line 165) Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/display (line 66) ans = desc: My first FINTS freq: Daily (1) 'dates: (4)' 'times: (4)' 'Data1: (4)' '01-Jan-2001' '12:00' [ 2] '02-Jan-2001' '11:00' [ 3] ' " ' '12:00' [ 4] '03-Jan-2001' '11:00' [ 5]
Treat timeday
as a MATLAB structure if you want to obtain the contents of a specific field. For
example, to find the times of day included in this object, enter
datestr(timeday.times)
Warning: FINTS will be removed in a future release. Use TIMETABLE instead. > In fints/subsref (line 106) ans = 11:00 AM 12:00 PM 11:00 AM 12:00 PM 11:00 AM 12:00 PM
See Also
fints
| ascii2fts
| fts2mat
| datestr
| ftsbound
| boxcox
| diff
| fillts
| filter
| lagts
| leadts
| peravg
| smoothts
| tsmovavg
| convertto
| resamplets
| toannual
| todaily
| tomonthly
| toquarterly
| tosemi
| toweekly