|
xlswrite
Write Microsoft Excel spreadsheet file (.xls)
Syntax
xlswrite(filename, M)
xlswrite(filename, M, sheet)
xlswrite(filename, M, 'range')
xlswrite(filename, M, sheet, 'range')
status = xlswrite(filename, ...)
[status, message] = xlswrite(filename, ...)
xlswrite filename M sheet range
Description
xlswrite(filename, M) writes matrix M to the Excel file filename. The filename input is a string enclosed in single quotes. 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:
*
A cell designation, such as 'D2', to indicate the upper left corner of the region to receive the matrix data.
*
Two cell designations separated by a colon, such as 'D2:H4', to indicate two opposing corners of the region to receive the matrix data. The range 'D2:H4' represents the 3-by-5 rectangular region between the two corners D2 and H4 on the worksheet.
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:
*
message — String containing the text of the warning or error message
*
identifier — String containing the message identifier for the warning or error
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').
Note The above functionality depends upon having Microsoft Excel as a COM server. In absence of Excel, matrix M is written as a text file in Comma-Separated Value (CSV) format. In this mode, the sheet and range arguments are ignored.
Examples
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:
xlswrite('testdata', [12.7 5.02 -98 63.9 0 -.2 56])
Example 2 — Writing Mixed Data to a Specific Worksheet
This example writes the following mixed text and numeric data to the file tempdata.xls:
d = {'Time', 'Temp'; 12 98; 13 99; 14 97};
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:
s = xlswrite('tempdata.xls', d, 'Temperatures', 'E1')
s =
1
The output status s shows that the write operation succeeded. The data appears as shown here in the output file:
Time Temp
12 98
13 99
14 97
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:
xlswrite('tempdata.xls', d, 'NewTemp', 'E1')
Warning: Added specified worksheet.
If you don't want to see these warnings, you can turn them off using the command indicated in the message above:
warning off MATLAB:xlswrite:AddSheet
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:
[stat msg] = xlswrite('tempdata.xls', d, 'NewTemp2', 'E1');
msg
msg =
message: 'Added specified worksheet.'
identifier: 'MATLAB:xlswrite:AddSheet'
See Also |
|