num = xlsread(filename) reads
data from the first worksheet in the Microsoft® Excel® spreadsheet
file named filename and returns the numeric data
in array num.
On Windows® systems with Microsoft Excel software, xlsread reads
any file format recognized by your version of Excel.
If your system does not have Excel for Windows, xlsread operates
in basic import mode, and reads only XLS, XLSX,
XLSM, XLTX, and XLTM files.
num = xlsread(filename,sheet,xlRange,'basic') reads
data from the spreadsheet in basic mode, the default
on systems without Excel for Windows. If you do not specify
all the arguments, use empty strings as placeholders, for example, num
= xlsread(filename,'','','basic').
[num,txt,raw]
= xlsread(___) additionally returns the text
fields in cell array txt, and the unprocessed
data (numbers and text) in cell array raw using
any of the input arguments in the previous syntaxes. If xlRange is
specified, leading blank rows and columns in the worksheet that precede
rows and columns with data are returned in raw.
___ = xlsread(filename,-1) opens
an Excel window to interactively select data. Select the worksheet,
drag and drop the mouse over the range you want, and click OK.
This syntax is supported only on Windows systems with Excel software.
[num,txt,raw,custom]
= xlsread(filename,sheet,xlRange,'',functionHandle) reads
from the spreadsheet, executes the function associated with functionHandle on
the data, and returns the final results as numeric data in array num.
The xlsread function optionally returns the text
fields in cell array txt, the unprocessed data
(numbers and text) in cell array raw, and the second
output from the function associated with functionHandle in
array custom. The xlsread function
does not change the data stored in the spreadsheet. This syntax is
supported only on Windows systems with Excel software.
In the Editor, create a function to process data from
a worksheet. In this case, set values outside the range [-3,
3] to -3 or 3.
function [Data] = setMinMax(Data)
minval = -3; maxval = 3;
for k = 1:Data.Count
v = Data.Value{k};
if v > maxval || v < minval
if v > maxval
Data.Value{k} = maxval;
else
Data.Value{k} = minval;
end
end
end
In the Command Window, add data to myExample.xlsx.
Read the data from the worksheet, and reset any values
outside the range [-3, 3]. Specify the sheet name,
but use '' as placeholders for the xlRange and 'basic' inputs.
trim = xlsread('myExample.xlsx','MyData','','',@setMinMax)
Execute a function on a worksheet and display
the custom index output.
In the Editor, modify the function setMinMax from
the previous example to return the indices of the changed elements
(custom output).
function [Data, indices] = setMinMax(Data)
minval = -3; maxval = 3;
indices = [];
for k = 1:Data.Count
v = Data.Value{k};
if v > maxval || v < minval
if v > maxval
Data.Value{k} = maxval;
else
Data.Value{k} = minval;
end
indices = [indices k];
end
end
Read the data from the worksheet MyData,
and request the custom index output, idx.
Name of the file to read, specified as a string. If you do not
include an extension, xlsread searches for a file
with the specified name and a supported Excel extension. xlsread can read data saved in files
that are currently open in Excel for Windows.
Worksheet to read, specified as one of the following:
String that contains the worksheet 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. Not supported for XLS files in basic mode.
Rectangular portion of the worksheet to read, specified as a
string.
Specify xlRange using the syntax 'C1:C2',
where C1 and C2 are 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').
If you specify sheet, then xlRange:
Does not need to include a colon and opposite corner
to describe a single cell.
Can refer to a named range that you defined in the Excel file
(see the Excel help).
Handle to a custom function, starting with the symbol @.
Supported only on Windows systems with Excel software. xlsread reads
from the spreadsheet, executes your function on a copy of the data,
and returns the final results. xlsread does not
change the data stored in the spreadsheet.
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 the Examples.)
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 strings, '',
in txt.
For XLS files in basic import mode, the txt output
contains empty strings, '', 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.
Unprocessed data from the worksheet, returned as a cell array.
Contains both numeric and text data.
On systems with Excel for Windows, undefined
values (such as '#N/A') appear in the raw output
as 'ActiveX VT_ERROR:'. For XLSX, XLSM, XLTX, and
XLTM files on other systems, undefined values appear as '#N/A'.