Write Microsoft Excel spreadsheet


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


xlswrite(filename, M)writes
matrixMto the Excel filefilename.
Thefilenameinput is a string enclosed in single
quotation marks, and should include the file extension. The matrixMis
anm-by-nnumeric or character
array or, if each cell contains a single element, a cell array (see Example
2).xlswritewrites the matrix data to the
first worksheet in the file, starting at cellA1.

Iffilenamedoes not exist,xlswritecreates
a new file. The file extension you provide as part offilenamedetermines
the Excel format thatxlswriteuses for the
new file. An extension of.xlscreates a worksheet
compatible with Excel 97-2003 software. Use extensions.xlsx,.xlsb,
or.xlsmto create worksheets in Excel 2007
file formats. The maximum size of the matrixMdepends
on the associated Excel version. (For more information on Excel specifications
and limits, see Excel help.)

xlswrite(filename, M, sheet)writes
matrixMto the specified worksheetsheetin
the filefilename. Thesheetargument
can be either a positive, double scalar value representing the worksheet
index, or a quoted string containing the sheet name. Thesheetargument
cannot contain a colon.

Ifsheetdoes not exist,xlswriteadds
a new sheet at the end of the worksheet collection. Ifsheetis
an index larger than the number of worksheets,xlswriteappends
empty sheets until the number of worksheets in the workbook equalssheet.
In either case,xlswritegenerates a warning indicating
that it has added a new worksheet.

xlswrite(filename, M, range)writes
matrixMto a rectangular region specified byrangein
the first worksheet of the filefilename.

Specifyrangeusing the syntax'C1:C2',
whereC1andC2are two opposing
corners that define the region to write. For example, the range'D2:H4'represents
the 3-by-5 rectangular region between the two cornersD2andH4on
the worksheet. Therangeinput is not case sensitive
and uses the Excel A1 reference style. (For more information
on this reference style, see Excel help.)xlswritedoes
not recognize named ranges.

The size defined byrangeshould fit the
size ofM. Ifrangeis larger
than the size ofM, Excel software fills the
remainder of the region with#N/A. Ifrangeis
smaller than the size ofM,xlswritewrites
only the submatrix that fits intorangeto the
file specified byfilename.

    If you specify only three inputs,xlswritemust
    decide whether the third input refers to asheetor
    arange. To specify arange,
    include a colon character in the input string (such as'D2:H4').
    If you do not include a colon character (such as'sales'or'D2'),xlswriteinterprets
    the third input as a value forsheet.

xlswrite(filename, M, sheet, range)writes
matrixMto a rectangular region specified byrangein
worksheetsheetof the filefilename.
If you specify bothsheetandrange,
therangecan either fit the size ofMor
contain only the first cell (such as'A2'). See
the previous two syntax formats for further explanation of thesheetandrangeinputs.

status = xlswrite(filename, ...)returns
the completion status of the write operation instatus.
If the write completes successfully,statusis
equal to logical1(true). Otherwise,statusis
logical0(false). Unless you
specify an output parameter,xlswritedoes not
display a status value in the Command Window.

[status, message] = xlswrite(filename,
...)returns any warning or error message generated by
the write operation in the MATLAB structuremessage.
Themessagestructure 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 rangeis
the command format forxlswrite, showing its
usage with all input arguments specified. When using this format,
you must specifysheetas a string (for example,IncomeorSheet4).
If thesheetname contains space characters, then
you must place quotation marks around the string (for example,'Income


Excel convertsInfvalues to65535.
MATLAB convertsNaNvalues to empty cells.

If your system does not have Excel for Windows installed,
or if the COM server (part of the typical installation of Excel for Windows)
is unavailable,xlswrite:

  • Writes matrixMas a text file
    in comma-separated value (CSV) format.

  • Ignores thesheetandrangearguments.

  • Generates an error if the input matrixMis
    a cell array.

If your system has Microsoft Office 2003 software installed,
but you want to create a file in an Excel 2007 format, you must
install the Office 2007 Compatibility Pack.

Numeric Dates

Both Excel and MATLAB applications represent numeric
dates as a number of serial days elapsed from a specific reference
date. However, Excel and MATLAB use different reference

Application Reference Date
MATLAB January 0, 0000
Excel for Windows January 1, 1900
Excel for the Macintosh January 2, 1904

For more information, see Converting Dates in
the MATLAB Data Import and Export documentation.


Example 1 — Writing Numeric Data to the Default Worksheet

Write a 7-element vector to Microsoft Excel filetestdata.xls.
By default,xlswritewrites the data to cellsA1throughG1in
the first worksheet in the file:

xlswrite('testdata.xls', [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 filetempdata.xls:

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

Callxlswrite, specifying the worksheet
labeledTemperatures, and the region within the
worksheet to write the data to.xlswritewrites
the 4-by-2 matrix to the rectangular region that starts at cellE1in
its upper left corner:

s = xlswrite('tempdata.xls', d, 'Temperatures', 'E1')
s =

The output statussshows that the write
operation succeeded. The data appears as shown here in the output

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
intempdata.xls. In this case,xlswriteappends
a new sheet to the workbook, calling it by the name you supplied in
thesheetsinput argument,'NewTemp'.xlswritedisplays
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
with this command:

warning off MATLAB:xlswrite:AddSheet

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

[stat msg] = xlswrite('tempdata.xls', d, 'NewTemp2', 'E1');

msg = 
       message: 'Added specified worksheet.'
    identifier: 'MATLAB:xlswrite:AddSheet'

标签: none