Contents Previous Next Subchapters

Writing Excel Data
Syntax xlwrite(fileNamevarName)
xlwrite(
filevarNamesheetName)
xlwrite(
filevarNamesheetNamexlsRange)
See Also xlread , xlsheets , read4 , bread

Overview
Create or update Microsoft Excel files. The character row vector fileName specifies the file name to be used in Excel. If fileName does not exist it is created, otherwise it is opened. The argument varName must be an expression that returns a character, integer, real, or double-precision matrix. By default, the xlwrite function copies data to Excel starting in cell A1 of the current or specified work sheet. The character row vector, xlsRange may be used to specify an alternate location for the transferred data.

Examples

Creating and Updating Numeric Excel Files
If at the O-Matrix prompt you enter,
    xlFile = "C:\temp\tst.xls"
    rmfile(xlFile);
    xlwrite(xlFile, rand(4,2))
O-Matrix will create a new Excel file that contains 4 rows and 2 columns of numeric data starting in cell A1 of the first sheet. If you continue the example above by entering
    xlwrite(xlFile, seq(10), "MySheet")
O-Matrix will open the existing tst.xls file, create a new work sheet named MySheet, and add 10 rows of data starting in cell A1.

Using Excel Range Specifications
By default, O-Matrix transfers data to the first column and the first row of the first worksheet, or the worksheet specified by sheetName. If present, the character row vector xlsRange can be used to specify the cells to use for the transferred data. If you continue the example above by entering
     xlwrite(xlFile, identity(3), "MySheet", "D4:F6")
O-Matrix will open the tst.xls file and add the 3 row by 3 column identity matrix to the MySheet worksheet starting in row 4 of column 4.

Transferring Character Data to Excel
Character vectors are transferred to individual cells in Excel. By default a character row vector is transferred to the first cell of the first worksheet. If at the O-Matrix prompt you enter
    xlFile = "c:\temp\chars.xls"
    rmfile(xlFile);
    xlwrite(xlFile, "Hello World")
O-Matrix will create a new Excel file that contains the string "Hello World" in the first row of the first column in Sheet1. Character range values are specified as a single cell If you continue the example by entering
    xlwrite(xlFile, "Greetings", "Characters", "B2")
O-Matrix will open the chars.xls file, create a new worksheet named Characters and write the string Greetings in the second row of the second column.