MATLAB Function Reference Previous page   Next Page

Write Microsoft Excel spreadsheet file (.xls)



xlswrite('filename', M) writes matrix M to the Excel file filename. The input matrix M is an m-by-n numeric, character, or cell array, where m < 65536 and n < 256. The matrix data is written to the first worksheet in the file, starting at cell A1.

xlswrite('filename', M, sheet) writes matrix M to the specified worksheet sheet in the file filename. The sheet argument can be either a positive, double scalar value representing the worksheet index, or a quoted string containing the sheet name.

If sheet does not exist, a new sheet is added at the end of the worksheet collection. If sheet is an index larger than the number of worksheets, empty sheets are appended until the number of worksheets in the workbook equals sheet. In either case, MATLAB generates a warning indicating that it has added a new worksheet.

xlswrite('filename', M, 'range') writes matrix M to a rectangular region specified by range in the first worksheet of the file filename. Specify range using one of the following quoted string formats:

The range input is not case sensitive and uses Excel A1 notation. (See help in Excel for more information on this notation.)

The size defined by range should fit the size of M or contain only the first cell, (e.g., 'A2'). If range is larger than the size of M, Excel fills the remainder of the region with #N/A. If range is smaller than the size of M, only the submatrix that fits into range is written to the file specified by filename.

xlswrite('filename', M, sheet, 'range') writes matrix M to a rectangular region specified by range in worksheet sheet of the file filename. See the previous two syntax formats for further explanation of the sheet and range inputs.

status = xlswrite('filename', ...) returns the completion status of the write operation in status. If the write completed successfully, status is equal to logical 1 (true). Otherwise, status is logical 0 (false). Unless you specify an output for xlswrite, no status is displayed in the Command Window.

[status, message] = xlswrite('filename', ...) returns any warning or error message generated by the write operation in the MATLAB structure message. The message structure has two fields:

xlswrite filename M sheet range is the command format for xlswrite, showing its usage with all input arguments specified. When using this format, you must specify sheet as a string (for example, Income or Sheet4). If the sheet name contains space characters, then quotation marks are required around the string (for example, 'Income 2002').


Example 1 -- Writing Numeric Data to the Default Worksheet

Write a 7-element vector to Microsoft Excel file testdata.xls. By default, the data is written to cells A1 through G1 in the first worksheet in the file:

Example 2 -- Writing Mixed Data to a Specific Worksheet

This example writes the following mixed text and numeric data to the file tempdata.xls:

Call xlswrite, specifying the worksheet labeled Temperatures, and the region within the worksheet to write the data to. The 4-by-2 matrix will be written to the rectangular region that starts at cell E1 in its upper left corner:

The output status s shows that the write operation succeeded. The data appears as shown here in the output file:

Example 3 -- Appending a New Worksheet to the File

Now write the same data to a worksheet that doesn't yet exist in tempdata.xls. In this case, MATLAB appends a new sheet to the workbook, calling it by the name you supplied in the sheets input argument, 'NewTemp'. MATLAB displays a warning indicating that it has added a new worksheet to the file:

If you don't want to see these warnings, you can turn them off using the command indicated in the message above:

Now try the command again, this time creating another new worksheet, NewTemp2. Although the message is not displayed this time, you can still retrieve it and its identifier from the second output argument, m:

See Also

xlsread, xlsfinfo, wk1read, textread

Previous page  xlsread xmlread Next page

© 1994-2005 The MathWorks, Inc.