Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Write Data to Excel Spreadsheets

Write Tabular Data to Spreadsheet File

To export a table in the workspace to a Microsoft® Excel® spreadsheet file, use the writetable function. You can export data from the workspace to any worksheet in the file, and to any location within that worksheet. By default, writetable writes your table data to the first worksheet in the file, starting at cell A1.

For example, create a sample table of column-oriented data and display the first five rows.

load patients.mat
T = table(LastName,Age,Weight,Smoker);
T(1:5,:)
ans=5x4 table
     LastName     Age    Weight    Smoker
    __________    ___    ______    ______

    'Smith'       38     176       true  
    'Johnson'     43     163       false 
    'Williams'    38     131       false 
    'Jones'       40     133       false 
    'Brown'       49     119       false 

Write table T to the first sheet in a new spreadsheet file named patientdata.xlsx, starting at cell D1. To specify the portion of the worksheet you want to write to, use the Range name-value pair argument.

filename = 'patientdata.xlsx';
writetable(T,filename,'Sheet',1,'Range','D1')

By default, writetable writes the table variable names as column headings in the spreadsheet file.

To write the table T to the second sheet in the file without the table variable names, specify the name-value pair WriteVariableNames as false.

writetable(T,filename,'Sheet',2,'WriteVariableNames',false)

Write Numeric and Text Data to Spreadsheet File

To export a numeric array and a cell array to a Microsoft® Excel® spreadsheet file, use the xlswrite function. You can export data in individual numeric and text workspace variables 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.

For example, create a sample array of numeric data, A, and a sample cell array of text and numeric data, C.

A = magic(5)
C = {'Time', 'Temp'; 12 98; 13 'x'; 14 97}
A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9


C = 

    'Time'    'Temp'
    [  12]    [  98]
    [  13]    'x'   
    [  14]    [  97]

Write array A to the 5-by-5 rectangular region, E1:I5, on the first sheet in a new spreadsheet file named testdata.xlsx.

filename = 'testdata.xlsx';
xlswrite(filename,A,1,'E1:I5')

Write cell array C to a rectangular region that starts at cell B2 on a worksheet named Temperatures. When you specify the sheet, you can specify range using only the first cell.

xlswrite(filename,C,'Temperatures','B2');

xlswrite will display a warning because the worksheet, Temperatures, did not previously exist, but you can disable this warning.

Disable Warning When Adding New Worksheet

If the target worksheet does not exist in the file, then the writetable and xlswrite functions display this warning:

Warning: Added specified worksheet.

You can disable these warnings with this command:

warning('off','MATLAB:xlswrite:AddSheet')

Format Cells in Excel Files

To write data to Excel files on Windows® systems with custom formats (such as fonts or colors), access the COM server directly using actxserver rather than writetable or xlswrite. For example, Technical Solution 1-QLD4K uses actxserver to establish a connection between MATLAB® and Excel, write data to a worksheet, and specify the colors of the cells.

For more information, see Getting Started with COM.

See Also

|

Was this topic helpful?