Difference between revisions of "LoadingData"

From ECLR
Jump to: navigation, search
Line 23: Line 23:
 
Generally MATLAB will generate the data in two matrices, one for the columns that MATLAB recognised as numerical data (often called <source enclose=none>data</source>) and another that contains all non-numerical spreadsheet entries (often called <source enclose=none>textdata</source>). You can then use these data matrices for any further operations. Date cells will end up in the <source enclose=none>textdata</source> matrix and you will have to convert them manually to the MATLAB dates files (see below).
 
Generally MATLAB will generate the data in two matrices, one for the columns that MATLAB recognised as numerical data (often called <source enclose=none>data</source>) and another that contains all non-numerical spreadsheet entries (often called <source enclose=none>textdata</source>). You can then use these data matrices for any further operations. Date cells will end up in the <source enclose=none>textdata</source> matrix and you will have to convert them manually to the MATLAB dates files (see below).
  
Instead of double clicking on a file in the "Current Directory" window you can also incorporate the data import into a piece of MATLAB script.
+
Instead of double clicking on a file in the "Current Directory" window you can also incorporate the data import into a piece of MATLAB script. This is done with the following command:
if text data then use command line:  
+
 
     [NUM,TXT,RAW]=xlsread(FILE,SHEET) as that will save the textdata in TXT and RAW
+
     [NUM,TXT,RAW]=xlsread('MSFT.xlsx')  
 +
 
 +
which will save the content of the EXCEL file (here called MSFT.xlsx) into three files. The complete spreadsheet is saved in <source enclose=none>RAW</source>, all numerical entries are in <source enclose=none>NUM</source> and all text entries are in <source enclose=none>TXT</source>.
  
 
== MATLAB R2012 and higher
 
== MATLAB R2012 and higher

Revision as of 16:41, 23 September 2012

Introduction

Usually you will have data saved in some file, like Excel files, csv (comma seperated values) file or a text file. These are the most common formats in which you can download data from various databases.

Before we can look at the data upload procedure it is important to have a very quick look at the way in which MATLAB deals with dates.

Dealing with date vectors

When dealing with time series data you will often want to keep date information. MATLAB has excellent date functionality and dealing with data is indeed one of its strengths. There are two principle formats in which MATLAB handles dates, the datenum and the datevec format

  1. The datenum format: Here MATLAB records date information in terms of the number of days since 01 Jan 0000. This day is given the number 1. The next day (2 Jan 0000) is assigned the number 2 and so forth. The 22 Sept 2012 is 735134. If you have time information included into your dates, that will be reflected in fractions. For example, '22-Sept-2012 12:00:00' is represented by 735134.5, as it is half way through the day.
  1. The datevec format: Here a date is transformed into a (1 x 6) vector where the first element represents the year, the second the month, the third the day, the fourth the hour, the fith the minutes and the sixth the seconds. If you only enter a day, the last three entries will take the value 0.

It is a great feature of MATLAB that it recognises a good number of different date formats and translates them easily into one of the two MATLAB formats. Importantly, if you open an EXCEL file (see below) and your spreadsheet contains dates formatted as EXCEL dates, MATLAB will automatically translate these dates into the datenum format if you are using a MATLAB version R2012a or higher.

Data Sources

By far the easiest formats to import data into MATLAB are Excel spreadsheets and csv files. When dealing with these type of files you can double click on the file you want to import in the "Current Directory" window. A window will then open and give you a preview of the data MATLAB will import. How exactly this wimdow looks like and what translation MATLAB does automatically now depens on the MATLAB version you are working with.

MATLAB R2011 and lower

Generally MATLAB will generate the data in two matrices, one for the columns that MATLAB recognised as numerical data (often called data) and another that contains all non-numerical spreadsheet entries (often called textdata). You can then use these data matrices for any further operations. Date cells will end up in the textdata matrix and you will have to convert them manually to the MATLAB dates files (see below).

Instead of double clicking on a file in the "Current Directory" window you can also incorporate the data import into a piece of MATLAB script. This is done with the following command:

   [NUM,TXT,RAW]=xlsread('MSFT.xlsx') 

which will save the content of the EXCEL file (here called MSFT.xlsx) into three files. The complete spreadsheet is saved in RAW, all numerical entries are in NUM and all text entries are in TXT.

== MATLAB R2012 and higher