Xlswrite
Xlswrite
Xlswrite
Syntax
xlswrite(filename,A)
xlswrite(filename,A,sheet)
xlswrite(filename,A,xlRange)
xlswrite(filename,A,sheet,xlRange)
status = xlswrite(___)
[status,message] = xlswrite(___)
Description
Examples
filename = 'testdata.xlsx';
A = [12.7 5.02 -98 63.9 0 -.2 56];
xlswrite(filename,A)
Write to Specific Sheet and Range in Spreadsheet
Write mixed text and numeric data to an Excel® file starting at cell E1 of
Sheet2.
filename = 'testdata.xlsx';
A = {'Time','Temperature'; 12,98; 13,99; 14,97};
sheet = 2;
xlRange = 'E1';
xlswrite(filename,A,sheet,xlRange)
Input Arguments
If filename does not exist, xlswrite creates a file, determining the format
based on the specified extension. To create a file compatible with Excel 97-2003
software, specify an extension of .xls. To create files in Excel 2007 formats,
specify an extension of .xlsx, .xlsb, or .xlsm. If you do not specify an
extension, xlswrite uses the default, .xls.
Example: 'myFile.xlsx'
Example: 'C:\myFolder\myFile.xlsx'
A — Input matrix
matrix
If A is a cell array containing something other than a scalar numeric or text, then
xlswrite silently leaves the corresponding cell in the spreadsheet empty.
The maximum size of array A depends on the associated Excel version. For more
information on Excel specifications and limits, see the Excel help.
Example: [10,2,45;-32,478,50]
Example: {92.0,'Yes',45.9,'No'}
Character vector that contains the worksheet name. The name cannot contain
a colon (:). To determine the names of the sheets in a spreadsheet file, use
xlsfinfo.
Positive integer that indicates the worksheet index.
If sheet does not exist, xlswrite adds a new sheet at the end of the worksheet
collection. If sheet is an index larger than the number of worksheets, xlswrite
appends empty sheets until the number of worksheets in the workbook equals
sheet. In either case, xlswrite generates a warning indicating that it has added
a new worksheet.
Specify xlRange using two opposing corners that define the region to write. For
example, 'D2:H4' represents the 3-by-5 rectangular region between the two
corners D2 and H4 on the worksheet. The xlRange input is not case sensitive,
and uses Excel A1 reference style (see Excel help). xlswrite does not recognize
named ranges.
If you do not specify sheet, then xlRange must include both corners and
a colon character, even for a single cell (such as 'D2:D2'). Otherwise,
xlswrite interprets the input as a worksheet name (such as 'D2').
If you specify sheet, then xlRange can specify only the first cell (such as
'D2'). xlswrite writes input array A beginning at this cell.
If xlRange is larger than the size of input array A, Excel software fills the
remainder of the region with #N/A. If xlRange is smaller than the size of
A, then xlswrite writes only the subset that fits into xlRange to the file.
Output Arguments
Status of the write operation, returned as either 1 (true) or 0 (false). When the
write operation is successful, status is 1. Otherwise, status is 0.
Limitations
If your computer does not have Excel for Windows®, or if the COM server
(part of the typical installation of Excel) is unavailable, then the xlswrite
function:
o Writes array A to a text file in comma-separated value (CSV) format.
A must be a numeric matrix.
o Ignores the sheet and xlRange arguments.
More About
Tips
If your computer has Microsoft Office 2003 software, but you want to create
a file in an Excel 2007 format, install the Office 2007 Compatibility Pack.
Excel and MATLAB® can store dates as text that represents those dates
(such as '10/31/96') or serial date numbers (such as 729329). If your
array includes serial date numbers, convert these dates to their text
representation using datestr before calling xlswrite. Alternatively,
see Import and Export Dates to Excel Files.
To write data to Excel files with custom formats (such as fonts or colors),
access the Windows COM server directly using actxserver rather than
xlswrite. For example, this MathWorks Support Answer uses
actxserver to establish a connection between MATLAB and Excel,
writes data to a worksheet, and specifies the colors of the cells.
xlsread
Read Microsoft Excel spreadsheet file
Syntax
num = xlsread(filename)
num = xlsread(filename,sheet)
num = xlsread(filename,xlRange)
num = xlsread(filename,sheet,xlRange)
num = xlsread(filename,sheet,xlRange,'basic')
[num,txt,raw] = xlsread(___)
___ = xlsread(filename,-1)
[num,txt,raw,custom] =
xlsread(filename,sheet,xlRange,'',processFcn)
Description
If you do not specify all the arguments, use empty character vectors, '' , as
placeholders, for example, num = xlsread(filename,'','','basic').
[num,txt,raw] = xlsread(___) additionally returns the text fields in cell
array txt, and both numeric and text data in cell array raw, using any of the input
arguments in the previous syntaxes.
[num,txt,raw,custom] =
xlsread(filename,sheet,xlRange,'',processFcn), where
processFcn is a function handle, reads from the spreadsheet, calls
processFcn on the data, and returns the final results as numeric data in array
num. The xlsread function returns the text fields in cell array txt, both the
numeric and text data in cell array raw, and the second output from processFcn
in array custom. The xlsread function does not change the data stored in the
spreadsheet. This syntax is supported only on Windows computers with Excel
software.
Examples
filename = 'myExample.xlsx';
A = xlsread(filename)
A =
1 2 3
4 5 NaN
7 8 9
Read a specific range of data from the Excel file in the previous example.
filename = 'myExample.xlsx';
sheet = 1;
xlRange = 'B2:C3';
subsetA = xlsread(filename,sheet,xlRange)
subsetA =
2 3
5 NaN
Read Column
Read the second column from the Excel file in the first example.
filename = 'myExample.xlsx';
columnB = xlsread(filename,'B:B')
columnB =
2
5
8
For better performance, include the row numbers in the range, such as 'B1:B3'.
Request the numeric data, text data, and combined data from the Excel file in the
first example.
[num,txt,raw] = xlsread('myExample.xlsx')
num =
1 2 3
4 5 NaN
7 8 9
txt =
'First' 'Second' 'Third'
'' '' ''
'' '' 'x'
raw =
'First' 'Second' 'Third'
[ 1] [ 2] [ 3]
[ 4] [ 5] 'x'
[ 7] [ 8] [ 9]
In the Editor, create a function to process data from a worksheet. In this case, set
values outside the range [0.2,0.8] to 0.2 or 0.8.
minval = 0.2;
maxval = 0.8;
for k = 1:Data.Count
v = Data.Value{k};
if v > maxval
Data.Value{k} = maxval;
elseif v < minval
Data.Value{k} = minval;
end
end
A = rand(5);
xlswrite('myExample.xlsx',A,'MyData')
The worksheet named MyData contains values ranging from 0 to 1.
Read the data from the worksheet, and reset any values outside the range
[0.2,0.8]. Specify the sheet name, but use '' as placeholders for the
xlRange and 'basic' inputs.
trim =
xlsread('myExample.xlsx','MyData','','',@setMinMax);
In the Editor, modify the function setMinMax from the previous example to
return the indices of the changed elements (custom output).
minval = 0.2;
maxval = 0.8;
indices = [];
for k = 1:Data.Count
v = Data.Value{k};
if v > maxval
Data.Value{k} = maxval;
indices = [indices k];
elseif v < minval
Data.Value{k} = minval;
indices = [indices k];
end
end
Read the data from the worksheet MyData, and request the custom index output,
idx.
[trim,txt,raw,idx] = xlsread('myExample.xlsx',...
'MyData','','',@setMinMax);
Input Arguments
Example: 'myFile.xlsx'
Character vector that contains the worksheet name. The name cannot contain
a colon (:). To determine the names of the sheets in a spreadsheet file, use
xlsfinfo. For XLS files in basic mode, sheet is case sensitive.
Positive integer that indicates the worksheet index. This option is not
supported for XLS files in basic mode.
Specify xlRange using two opposing corners that define the region to read. For
example, 'D2:H4' represents the 3-by-5 rectangular region between the two
corners D2 and H4 on the worksheet. The xlRange input is not case sensitive,
and uses Excel A1 reference style (see Excel help).
Range selection is not supported when reading XLS files in basic mode. In this
case, use '' in place of xlRange.
If you do not specify sheet, then xlRange must include both corners and a
colon character, even for a single cell (such as 'D2:D2'). Otherwise, xlsread
interprets the input as a worksheet name (such as 'sales' or 'D2').
basic mode is the default for computers without Excel for Windows. In basic
mode, xlsread:
When xlsread calls the custom function, it passes a range interface from the
Excel application to provide access to the data. The custom function must include
this interface both as an input and output argument. (See Execute a Function on a
Worksheet)
Example: @myFunction
Output Arguments
Numeric data, returned as a matrix of double values. The array does not contain
any information from header lines, or from outer rows or columns that contain
nonnumeric data. Text data in inner spreadsheet rows and columns appear as NaN
in the num output.
Text data, returned as a cell array. Numeric values in inner spreadsheet rows and
columns appear as empty character vectors, '', in txt.
For XLS files in basic import mode, the txt output contains empty character
vectors, '', in place of leading columns of numeric data that precede text data in
the spreadsheet. In all other cases, txt does not contain these additional columns.
Undefined values (such as '#N/A') appear in the txt output as '#N/A', except
for XLS files in basic mode.
Numeric and text data from the worksheet, returned as a cell array.