Vous êtes sur la page 1sur 6

Read Microsoft Excel spreadsheet le - MATLAB x...

http://in.mathworks.com/help/matlab/ref/xlsread....

xlsread
Read Microsoft Excel spreadsheet file

Syntax
num = xlsread(filename)
example

num = xlsread(filename,sheet)
num = xlsread(filename,xlRange)
example

num = xlsread(filename,sheet,xlRange)
example

num = xlsread(filename,sheet,xlRange,'basic')
[num,txt,raw] = xlsread( ___ )
example
___

= xlsread(filename,-1)

[num,txt,raw,custom] = xlsread(filename,sheet,xlRange,'',functionHandle)
example

Description
example

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) reads the specified worksheet.
example

num = xlsread(filename,xlRange) reads data from the specified range, xlRange, of the first

worksheet in the file.


example

num = xlsread(filename,sheet,xlRange) reads from the specified sheet and range,


xlRange.
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').
example

1 of 6

[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.
Monday 24 November 2014 06:53 PM

Read Microsoft Excel spreadsheet le - MATLAB x...


http://in.mathworks.com/help/matlab/ref/xlsread....
___ = 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.
example

[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.

Examples
collapse all
Read Data from First Worksheet Into Numeric Array
Create an Excel file named myExample.xlsx.

values = {1, 2, 3 ; 4, 5, 'x' ; 7, 8, 9};


headers = {'First','Second','Third'};
xlswrite('myExample.xlsx', [headers; values]);
Sheet1 of myExample.xlsx contains:

First
1
4
7

Second
2
5
8

Third
3
x
9

Read data from the first worksheet.

filename = 'myExample.xlsx';
A = xlsread(filename)
A =
1
4
7

2
5
8

3
NaN
9

xlsread returns the numeric data in array A.

Read a Specific Range of Data


Read a specific range of data from the Excel file in the previous example.

2 of 6

filename = 'myExample.xlsx';
sheet = 1;

Monday 24 November 2014 06:53 PM

Read Microsoft Excel spreadsheet le - MATLAB x...

http://in.mathworks.com/help/matlab/ref/xlsread....

xlRange = 'B2:C3';
subsetA = xlsread(filename, sheet, xlRange)
subsetA =
2
3
5
NaN

Read a Column of Data


Read the second column of data from the Excel file in the first example.

filename = 'myExample.xlsx';
columnB = xlsread(filename,'B:B')
columnB =
2
5
8
For better performance, specify the row numbers in the range, as shown in the previous example.

Request Numeric, Text, and Unprocessed Data


Request the numeric data, text, and a copy of the unprocessed (raw) data from the Excel file in the first example.

[ndata, text, alldata] = xlsread('myExample.xlsx')


ndata =
1
4
7

3 of 6

2
5
8

3
NaN
9

text =
'First'
''
''

'Second'
''
''

'Third'
''
'x'

alldata =
'First'
[
1]
[
4]
[
7]

'Second'
[
2]
[
5]
[
8]

'Third'
[
3]
'x'
[
9]

Monday 24 November 2014 06:53 PM

Read Microsoft Excel spreadsheet le - MATLAB x...


http://in.mathworks.com/help/matlab/ref/xlsread....
xlsread returns numeric data in array ndata, text data in cell array text, and unprocessed data in cell array
alldata.

Execute a Function on a Worksheet and Return Numeric Data


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.

misc = pi*gallery('normaldata',[10,3],1);
xlswrite('myExample.xlsx',misc,'MyData');
Worksheet MyData contains the following values, which range from -6.6493 to 3.4845:

2.7156
0.2959
-2.6764
2.7442
-1.3761
-1.3498
-3.4643
1.2448
-3.0314
0.5292

-6.1744
-2.3383
-1.7351
-2.5752
3.4845
-1.9319
-0.8000
-0.8477
-5.2527
-5.8938

1.8064
-2.7210
-6.6493
-3.0300
0.6683
1.5014
0.3162
0.9344
1.7912
-5.1035

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.

4 of 6

trim = xlsread('myExample.xlsx','MyData','','',@setMinMax)
Monday 24 November 2014 06:53 PM

Read Microsoft Excel spreadsheet le - MATLAB x...

trim =
2.7156
0.2959
-2.6764
2.7442
-1.3761
-1.3498
-3.0000
1.2448
-3.0000
0.5292

-3.0000
-2.3383
-1.7351
-2.5752
3.0000
-1.9319
-0.8000
-0.8477
-3.0000
-3.0000

http://in.mathworks.com/help/matlab/ref/xlsread....

1.8064
-2.7210
-3.0000
-3.0000
0.6683
1.5014
0.3162
0.9344
1.7912
-3.0000

Request Custom Output


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.

[trim,txt,raw,idx] = xlsread('myExample.xlsx',...
'MyData','','',@setMinMax);
disp(idx)
7
5 of 6

11

15

19

20

23

24

30
Monday 24 November 2014 06:53 PM

Read Microsoft Excel spreadsheet le - MATLAB x...

Input Arguments
expand
all
filename

http://in.mathworks.com/help/matlab/ref/xlsread....

Name of file to read

string
Worksheet to read
string | positive integer
sheet

xlRange

Rectangular portion of the worksheet to read

string
Flag to request reading in basic mode
literal string
'basic'

functionHandle

Handle to a custom function

function handle

OutputallArguments
expand
Numeric data
matrix
num

Text data
cell array
txt

Unprocessed data
cell array
raw

Second output of the function corresponding to functionHandle


defined by the function
custom

Limitations
xlsread reads only 7-bit ASCII characters.
xlsread does not support non-contiguous ranges.

More
expand About
all
Algorithms
Import and Export Dates to Excel Files

See Also
function_handle | importdata | readtable | uiimport | xlsfinfo | xlswrite

6 of 6

Monday 24 November 2014 06:53 PM

Vous aimerez peut-être aussi