Main Content

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=5×4 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. By default, writetable writes the table variable names as column headings in the spreadsheet file.

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

Write the table T without the variable names to a new sheet called 'MyNewSheet'. To write the data without the variable names, specify the name-value pair WriteVariableNames as false.

writetable(T,filename,'Sheet','MyNewSheet','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 writematrix or writecell functions. 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, the import functions write 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';
writematrix(A,filename,'Sheet',1,'Range','E1:I5')

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

writecell(C,filename,'Sheet','Temperatures','Range','B2');

writecell displays 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 writecell functions display this warning:

Warning: Added specified worksheet.

For information on how to suppress warning messages, see Suppress Warnings.

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, writetimetable, writematrix, or writecell. 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 Get Started with COM.

See Also

| |