|On this page…|
In both MATLAB® and Excel® applications, dates can be represented as character strings or numeric values. For example, May 31, 2009, can be represented as the character string '05/31/09' or as the numeric value 733924. Within MATLAB, the datestr and datenum functions allow you to convert easily between string and numeric representations.
If you import a spreadsheet with dates stored as strings on a system with Excel for Windows®, or if you use the Import Tool, you do not need to convert the dates before processing in MATLAB.
However, if you use xlsread or importdata to import a spreadsheet with dates stored as numbers, or if your system does not have Excel for Windows, you must convert the dates. Both Excel and MATLAB represent numeric dates as a number of serial days elapsed from a specific reference date, but the applications use different reference dates.
The following table lists the reference dates for MATLAB and Excel. For more information on the 1900 and 1904 date systems, see the Excel help.
|MATLAB||January 0, 0000|
|Excel for Windows||January 1, 1900|
|Excel for the Macintosh||January 2, 1904|
Consider the hypothetical file weight_log.xls with
Date Weight 10/31/96 174.8 11/29/96 179.3 12/30/96 190.4 01/31/97 185.7
To import this file, first convert the dates within Excel to a numeric format. In Windows, the file now appears as
Date Weight 35369 174.8 35398 175.3 35429 190.4 35461 185.7
Import the file:
wt = xlsread('weight_log.xls');
Convert the dates to the MATLAB reference date. If the file uses the 1900 date system (the default in Excel for Windows):
datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('30-Dec-1899');
If the file uses the 1904 date system (the default in Excel for the Macintosh):
datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('01-Jan-1904');