Importing Data

There are myriad ways of getting data into Matlab. We will highlight three: (1) importing data from an Excel file, (2) importing data directly from FRED, and (3) importing data directly from Yahoo Finance.

Importing Data from an Excel File

  • The real GDP data come from the Federal Reserve Economic Data provided by the Federal Reserve Bank of St. Louis.

  • To import data from an Excel file, make sure the file is located in the folder that is currently active in Matlab called “Current Folder”. Then click the “Import Data” button near the top of the window.
  • Select the file that you would like to import:
  • The next window will provide some options for the import.

  • If the data are not properly selected, you can change the cell selection in the “Range” cell. You can also change the variable names selected by changing the “Variable Names Row” cell.

  • The “Imported Data” block is used to select how you would like the data imported: as seperate vectors, as one matrix, as a cell array, or as a table. For our example, select “Column vectors”.

  • The “Convert” to “datenum” changes the excel value date to a Matlab language date. The “Replace” with “NaN” replaces any unimportable values as “Not a Number”.

  • Once you are happy with the selected fields, you can click the drop-down menu “Import Selection” to choose if you want Matlab to import the data directly, create a script to import the data, or create a function to import the data. These options can come in handy when you are writing long programs and you store the data seperate from the matlab.mat file.

  • For our example, let’s import the data directly by clicking on the green check mark above “Import Selection”.
  • You can see that both the date and real gdp have been imported as seperate vectors. Each vector has 270 rows and 1 column. You can double click either variable to view the individual values

Importing Data from FRED

  • Alternatively, you can import GDP data directly from FRED. First, assign any variable name equal to “fred”. Next, assign any variable name equal to “fetch(f,’GDP’)
  • Matlab downloads the data from FRED into what is known as a structure variable. To view the variables available within the structure, either type the name of the structure in the command menu or double click on the structure in the workspace.
  • To access any variable within the structure, simply type “variablename.subname”. In our case, we want to access the subvariable named “Data”. The first column of “GDP.Data” is a column vector of dates and the second column is a column vector of GDP observations

Importing Data from Yahoo Finance

  • To complete this section you will need to download the Historical Stock Data downloader at Matlab Central’s File Exchange. From this link you will just need to click on “Download Submission”.
  • Once you have downloaded the file, open it and place the function “hist_stock_data()” into your active Matlab “Current Folder”.
  • Then download S&P 500 price data for the year 2013. The function inputs are start date, end date, and ticker. The dates are in European format. The ticker is specific to Yahoo Finance. Once you have downloaded the price history, double click on the variable in your workspace to reveal all of the information you downloaded.
  • The data are saved in what Matlab calls a structure format. To access any of these values, simply type “variablename.subname”. For example, if you only want the price vector of daily closing values, then type “price.Close”

  • The data from Yahoo are downloaded in reverse order. To get a series of returns, dates and a price vector in the correct order enter the following code:
  • We can save these three series by simply typing “save”. The data will be stored in a file named “matlab.mat”. You can right-click on this file to change the name to “data” if you like. These three series will be used throughout most of the following sections.