Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Exporting to Excel Spreadsheets

Use xlswrite to export a matrix to an Excel spreadsheet file. With xlswrite, you can export data from the workspace to any worksheet in the file, and to any location within that worksheet. By default, xlswrite writes your matrix data to the first worksheet in the file, starting at cell A1.

xlswrite can write to any file format recognized by your version of Excel for Windows. If you have Excel 2003 installed, but want to write to a 2007 format (such as XLSX, XLSB, or XLSM), you must install the Office 2007 Compatibility Pack.

To write data to an Excel file, specify the name and extension of the output file in the call to xlswrite. If the file already exists, xlswrite writes data in the existent file format. If the file does not exist, xlswrite creates a new file, using the format that corresponds to the file extension you specify. If you do not specify a file extension, xlswrite applies the XLS extension, and writes a new file in the XLS format.

Example — Writing to an XLS File

This example writes a mix of text and numeric data to the file climate.xls. Call xlswrite, specifying a worksheet labeled Temperatures, and the region within the worksheet where you want to write the data. xlswrite writes the 4-by-2 matrix d to the rectangular region that starts at cell E1 in its upper-left corner:

d = {'Time', 'Temp'; 12 98; 13 99; 14 97}
d = 
    'Time'    'Temp'
    [  12]    [  98]
    [  13]    [  99]
    [  14]    [  97]

xlswrite('climate.xls', d, 'Temperatures', 'E1');

Adding a New Worksheet

If the target worksheet does not already exist in the file, xlswrite displays the following warning:

Warning: Added specified worksheet.

You can disable these warnings with this command:

warning off MATLAB:xlswrite:AddSheet

Converting Dates

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 export a matrix with dates stored as strings, you do not need to convert the dates before processing in Excel.

However, if you export a matrix with dates stored as numbers, 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.

ApplicationReference Date
MATLABJanuary 0, 0000
Excel for WindowsJanuary 1, 1900
Excel for the MacintoshJanuary 2, 1904

Example — Exporting to an Excel File with Numeric Dates

Consider a numeric matrix wt_log. The first column contains numeric dates, and the second column contains weights:

wt_log = [729698 174.8; ...
          729726 175.3; ...
          729760 190.4; ...
          729787 185.7];

% To view the dates before exporting, call datestr:
datestr(wt_log(:,1))

The formatted dates returned by datestr are:

04-Nov-1997
02-Dec-1997
05-Jan-1998
01-Feb-1998

To export the numeric matrix to Excel for Windows (and use the default 1900 date system), convert the dates:

datecol = 1;
wt_log(:,datecol) = wt_log(:,datecol) - datenum('30-Dec-1899');
xlswrite('new_log.xls', wt_log); 

To export for use in Excel for the Macintosh (with the default 1904 date system), convert as follows:

datecol = 1;
wt_log(:,datecol) = wt_log(:,datecol) - datenum('01-Jan-1904');
xlswrite('new_log.xls', wt_log); 
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS