Sunteți pe pagina 1din 7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

xlsread
ReadMicrosoftExcelspreadsheetfile

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,'',processFcn)

example

Description
num=xlsread(filename)readsthefirstworksheetintheMicrosoftExcelspreadsheetworkbook
namedfilenameandreturnsthenumericdatainamatrix.

example

num=xlsread(filename,sheet)readsthespecifiedworksheet.
num=xlsread(filename,xlRange)readsfromthespecifiedrangeofthefirstworksheetinthe
workbook.UseExcelrangesyntax,suchas'A1:C3'.

example

num=xlsread(filename,sheet,xlRange)readsfromthespecifiedworksheetandrange.

example

num=xlsread(filename,sheet,xlRange,'basic')readsdatafromthespreadsheetinbasicimport
mode.IfyourcomputerdoesnothaveExcelforWindows ,xlsreadautomaticallyoperatesinbasic
importmode,whichsupportsXLS,XLSX,XLSM,XLTX,andXLTMfiles.
Ifyoudonotspecifyallthearguments,useemptycharactervectors,'',asplaceholders,forexample,
num=xlsread(filename,'','','basic').
[num,txt,raw]=xlsread( ___ )additionallyreturnsthetextfieldsincellarraytxt,andbothnumeric
andtextdataincellarrayraw,usinganyoftheinputargumentsintheprevioussyntaxes.

example

___ =xlsread(filename,1)opensanExcelwindowtointeractivelyselectdata.Selecttheworksheet,
draganddropthemouseovertherangeyouwant,andclickOK.ThissyntaxissupportedonlyonWindows
computerswithMicrosoftExcelsoftwareinstalled.

[num,txt,raw,custom]=xlsread(filename,sheet,xlRange,'',processFcn),whereprocessFcnis
afunctionhandle,readsfromthespreadsheet,callsprocessFcnonthedata,andreturnsthefinalresults
asnumericdatainarraynum.Thexlsreadfunctionreturnsthetextfieldsincellarraytxt,boththe
numericandtextdataincellarrayraw,andthesecondoutputfromprocessFcninarraycustom.The
xlsreadfunctiondoesnotchangethedatastoredinthespreadsheet.Thissyntaxissupportedonlyon
WindowscomputerswithExcelsoftware.

Examples

example

collapseall

ReadWorksheetIntoNumericMatrix

https://in.mathworks.com/help/matlab/ref/xlsread.html

1/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

CreateanExcelfilenamedmyExample.xlsx.
values={1,2,3;4,5,'x';7,8,9};
headers={'First','Second','Third'};
xlswrite('myExample.xlsx',[headers;values]);
Sheet1ofmyExample.xlsxcontains:
FirstSecondThird
123
45x
789
Readnumericdatafromthefirstworksheet.
filename='myExample.xlsx';
A=xlsread(filename)
A=
123
45NaN
789

ReadRangeofCells

ReadaspecificrangeofdatafromtheExcelfileinthepreviousexample.
filename='myExample.xlsx';
sheet=1;
xlRange='B2:C3';
subsetA=xlsread(filename,sheet,xlRange)
subsetA=
23
5NaN

ReadColumn

ReadthesecondcolumnfromtheExcelfileinthefirstexample.
filename='myExample.xlsx';
columnB=xlsread(filename,'B:B')
columnB=
2
5
8
Forbetterperformance,includetherownumbersintherange,suchas'B1:B3'.

RequestNumeric,Text,andRawData

https://in.mathworks.com/help/matlab/ref/xlsread.html

2/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

Requestthenumericdata,textdata,andcombineddatafromtheExcelfileinthefirstexample.
[num,txt,raw]=xlsread('myExample.xlsx')
num=
123
45NaN
789
txt=
'First''Second''Third'
''''''
'''''x'
raw=
'First''Second''Third'
[1][2][3]
[4][5]'x'
[7][8][9]

ExecuteaFunctiononaWorksheet

IntheEditor,createafunctiontoprocessdatafromaworksheet.Inthiscase,setvaluesoutsidetherange
[0.2,0.8]to0.2or0.8.
function[Data]=setMinMax(Data)
minval=0.2;
maxval=0.8;

fork=1:Data.Count
v=Data.Value{k};
ifv>maxval
Data.Value{k}=maxval;
elseifv<minval
Data.Value{k}=minval;
end
end
IntheCommandWindow,addrandomdatatomyExample.xlsx.
A=rand(5);
xlswrite('myExample.xlsx',A,'MyData')
TheworksheetnamedMyDatacontainsvaluesrangingfrom0to1.
Readthedatafromtheworksheet,andresetanyvaluesoutsidetherange[0.2,0.8].Specifythesheetname,but
use''asplaceholdersforthexlRangeand'basic'inputs.
trim=xlsread('myExample.xlsx','MyData','','',@setMinMax);

https://in.mathworks.com/help/matlab/ref/xlsread.html

3/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

RequestCustomOutput

Executeafunctiononaworksheetanddisplaythecustomindexoutput.
IntheEditor,modifythefunctionsetMinMaxfromthepreviousexampletoreturntheindicesofthechanged
elements(customoutput).
function[Data,indices]=setMinMax(Data)
minval=0.2;
maxval=0.8;
indices=[];

fork=1:Data.Count
v=Data.Value{k};
ifv>maxval
Data.Value{k}=maxval;
indices=[indicesk];
elseifv<minval
Data.Value{k}=minval;
indices=[indicesk];
end
end
ReadthedatafromtheworksheetMyData,andrequestthecustomindexoutput,idx.
[trim,txt,raw,idx]=xlsread('myExample.xlsx',...
'MyData','','',@setMinMax);

InputArguments

collapseall

filenameFilename
charactervector

Filename,specifiedasacharactervector.Ifyoudonotincludeanextension,xlsreadsearchesforafilewiththe
specifiednameandasupportedExcelextension.xlsreadcanreaddatasavedinfilesthatarecurrentlyopenin
ExcelforWindows.
Example:'myFile.xlsx'
DataTypes:char

sheetWorksheet
charactervector|positiveinteger

Worksheet,specifiedasoneofthefollowing:

Charactervectorthatcontainstheworksheetname.Thenamecannotcontainacolon(:).Todeterminethe
namesofthesheetsinaspreadsheetfile,usexlsfinfo.ForXLSfilesinbasicmode,sheetiscasesensitive.

Positiveintegerthatindicatestheworksheetindex.ThisoptionisnotsupportedforXLSfilesinbasicmode.

https://in.mathworks.com/help/matlab/ref/xlsread.html

4/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

xlRangeRectangularrange
charactervector

Rectangularrange,specifiedasacharactervector.
SpecifyxlRangeusingtwoopposingcornersthatdefinetheregiontoread.Forexample,'D2:H4'representsthe3
by5rectangularregionbetweenthetwocornersD2andH4ontheworksheet.ThexlRangeinputisnotcase
sensitive,andusesExcelA1referencestyle(seeExcelhelp).
RangeselectionisnotsupportedwhenreadingXLSfilesinbasicmode.Inthiscase,use''inplaceofxlRange.
Ifyoudonotspecifysheet,thenxlRangemustincludebothcornersandacoloncharacter,evenforasinglecell
(suchas'D2:D2').Otherwise,xlsreadinterpretstheinputasaworksheetname(suchas'sales'or'D2').
Ifyouspecifysheet,thenxlRange:

Doesnotneedtoincludeacolonandoppositecornertodescribeasinglecell.

CanrefertoanamedrangethatyoudefinedintheExcelfile(seetheExcelhelp).

WhenthespecifiedxlRangeoverlapsmergedcells:

OnWindowscomputerswithExcel,xlsreadexpandstherangetoincludeallmergedcells.

OncomputerswithoutExcelforWindows,xlsreadreturnsdataforthespecifiedrangeonly,withemptyorNaN
valuesformergedcells.

DataTypes:char

'basic'Flagtorequestreadinginbasicmode
charactervector

Flagtorequestreadinginbasicmode,specifiedasthecharactervector,'basic'.
basicmodeisthedefaultforcomputerswithoutExcelforWindows.Inbasicmode,xlsread:

ReadsXLS,XLSX,XLSM,XLTX,andXLTMfilesonly.

DoesnotsupportanxlRangeinputwhenreadingXLSfiles.Inthiscase,use''inplaceofxlRange.

Doesnotsupportfunctionhandleinputs.

ImportsalldatesasExcelserialdatenumbers.Excelserialdatenumbersuseadifferentreferencedatethan
MATLABdatenumbers.

processFcnHandletoacustomfunction
functionhandle

Handletoacustomfunction.ThisargumentissupportedonlyonWindowscomputerswithExcelsoftware.xlsread
readsfromthespreadsheet,executesyourfunctiononacopyofthedata,andreturnsthefinalresults.xlsread
doesnotchangethedatastoredinthespreadsheet.
Whenxlsreadcallsthecustomfunction,itpassesarangeinterfacefromtheExcelapplicationtoprovideaccessto
thedata.Thecustomfunctionmustincludethisinterfacebothasaninputandoutputargument.(SeeExecutea
FunctiononaWorksheet)
Example:@myFunction

https://in.mathworks.com/help/matlab/ref/xlsread.html

5/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

OutputArguments

collapseall

numNumericdata
matrix

Numericdata,returnedasamatrixofdoublevalues.Thearraydoesnotcontainanyinformationfromheaderlines,
orfromouterrowsorcolumnsthatcontainnonnumericdata.Textdataininnerspreadsheetrowsandcolumns
appearasNaNinthenumoutput.

txtTextdata
cellarray

Textdata,returnedasacellarray.Numericvaluesininnerspreadsheetrowsandcolumnsappearasempty
charactervectors,'',intxt.
ForXLSfilesinbasicimportmode,thetxtoutputcontainsemptycharactervectors,'',inplaceofleading
columnsofnumericdatathatprecedetextdatainthespreadsheet.Inallothercases,txtdoesnotcontainthese
additionalcolumns.
Undefinedvalues(suchas'#N/A')appearinthetxtoutputas'#N/A',exceptforXLSfilesinbasicmode.

rawNumericandtextdata
cellarray

Numericandtextdatafromtheworksheet,returnedasacellarray.
OncomputerswithExcelforWindows,undefinedvalues(suchas'#N/A')appearintherawoutputas'ActiveX
VT_ERROR:'.ForXLSX,XLSM,XLTX,andXLTMfilesonothercomputers,undefinedvaluesappearas'#N/A'.

customSecondoutputofthefunctioncorrespondingtoprocessFcn
definedbythefunction

SecondoutputofthefunctioncorrespondingtoprocessFcn.Thevalueanddatatypeofcustomaredeterminedby
thefunction.

Limitations

xlsreadreadsonly7bitASCIIcharacters.

xlsreaddoesnotsupportnoncontiguousranges.

MoreAbout

collapseall

Algorithms

xlsreadimportsformattedtextrepresentingdates(suchas'10/31/96'),exceptinbasicmodeandon
computerswithoutExcelforWindows.

ImportandExportDatestoExcelFiles

https://in.mathworks.com/help/matlab/ref/xlsread.html

6/7

12/11/2016

ReadMicrosoftExcelspreadsheetfileMATLABxlsreadMathWorksIndia

CreateFunctionHandle

SeeAlso
importdata|readtable|uiimport|xlsfinfo|xlswrite

IntroducedbeforeR2006a

https://in.mathworks.com/help/matlab/ref/xlsread.html

7/7

S-ar putea să vă placă și