MATLAB Examples

# Retrieve Historical Data Using FRED®

This example shows how to connect to FRED®, retrieve historical foreign exchange rates, and determine when the highest rate occurred.

Connect to the FRED® data server using the URL 'https://fred.stlouisfed.org/'.

```url = 'https://fred.stlouisfed.org/'; c = fred(url); ```

Adjust the display data format for currency.

```format bank ```

Retrieve all historical data for the US / Euro Foreign Exchange Rate series.

```series = 'DEXUSEU'; d = fetch(c,series) ```
```d = struct with fields: Title: ' U.S. / Euro Foreign Exchange Rate' SeriesID: ' DEXUSEU' Source: ' Board of Governors of the Federal Reserve System (US)' Release: ' H.10 Foreign Exchange Rates' SeasonalAdjustment: ' Not Seasonally Adjusted' Frequency: ' Daily' Units: ' U.S. Dollars to One Euro' DateRange: ' 1999-01-04 to 2017-02-03' LastUpdated: ' 2017-02-06 3:52 PM CST' Notes: ' Noon buying rates in New York City for cable transfers payable in foreign currencies.' Data: [4720×2 double] ```

d contains the series description.

Display the numeric representation of the date and the foreign exchange rate for the first three rows of data.

```d.Data(1:3,:) ```
```ans = 730124.00 1.18 730125.00 1.18 730126.00 1.16 ```

Retrieve historical data from January 1 through June 1, 2012, for the US / Euro Foreign Exchange Rate series.

```startdate = '01/01/2012'; % beginning of date range for historical data enddate = '06/01/2012'; % ending of date range for historical data d = fetch(c,series,startdate,enddate) ```
```d = struct with fields: Title: ' U.S. / Euro Foreign Exchange Rate' SeriesID: ' DEXUSEU' Source: ' Board of Governors of the Federal Reserve System (US)' Release: ' H.10 Foreign Exchange Rates' SeasonalAdjustment: ' Not Seasonally Adjusted' Frequency: ' Daily' Units: ' U.S. Dollars to One Euro' DateRange: ' 1999-01-04 to 2017-02-03' LastUpdated: ' 2017-02-06 3:52 PM CST' Notes: ' Noon buying rates in New York City for cable transfers payable in foreign currencies.' Data: [110×2 double] ```

Determine the highest foreign exchange rate maxforex in the date range. forex contains all the exchange rates in the data.

```forex = d.Data(:,2); maxforex = max(forex) ```
```maxforex = 1.35 ```

Determine when the highest foreign exchange rate occurred. To find the index idx for the highest foreign exchange rate, the function find uses the tolerance value. Retrieve the serial date number by indexing into the array of data using idx. Convert the serial date number to a character vector using the datestr function.

```value = abs(forex-maxforex); idx = find(value<0.001,1); date = d.Data(idx,1); datestr(date) ```
```ans = '24-Feb-2012' ```

Close the FRED® connection.

```close(c) ```