Loading Files MATLAB
Loading Files MATLAB
Givenadatasamplesuchassample3.datbelow:
Figure1
TherearesomeoptionsofwaysofloadingsuchdatafilesintoMATLABasillustratedbelow:
1. Loadingthedatadirectly
Thisisparticularlyconvenientifthedataisallnumeric.TheninMATLAByousimplyissuethecommand:
>>loadsample3.dat
Thesystemrespondsbyechoingthefollowingdetailsinthecommandwindow.
sample3=
1.120027.3000
3.080037.6000
5.250046.0000
8.750050.6000
12.300056.1000
16.100059.2000
30.200065.0000
1|P a g e
Oncethedataisloadedyoucannowapplybuiltinoruserdefinedfunctionstocomputecharacteristics
ofthedata:
>>formatcompact
%Computethesquarerootoftheentriesofthedataset
>>sqrt(sample3)
ans=
1.05835.2249
1.75506.1319
2.29136.7823
2.95807.1134
3.50717.4900
4.01257.6942
5.49558.0623
%Computethemeanandstandarddeviationsfortheset
>>mean(sample3)
ans=
10.971448.8286
>>std(sample3)
ans=
9.956613.0554
2. Loadingandassigningavariablenametothedataset
Thisisparticularlyusefulifyouwouldliketotreatthevariouscolumnsofdatainthedatasetdifferently.
Asanexamplethecommandsusedinoption1aresummarized,thenexpandedinthefollowingscript.
Forthesakeofbrevityassignthedatainsample3.datthevariablenameD.Inthiscaseloadingthefile
hasadifferentsyntaxstructure.ExaminethefollowingMATLABscript.Forpracticereproducethescript
andrunitMATLABforpractice.
2|P a g e
3. Loadingmultiplerowsandcolumnsbutusingonlyasubsetofthedata
Thisoptionissimilartooption2aboveanditisparticularlyuseful.Themainstepistoopenthetextfile
fromMATLAB.Itcouldbeindifferenttextfileformatse.g.file.txt,file.dat,file.matetc.
Hereweloadsample3.datasshownearlierinFigure1.NoticeintheRangewindowofFigure1wesee
thattherangeofdataisindicatedasA1:B7.Thismeansifyouclickonthecheckmarkontherighthand
sideoftheImportationwindowtoImportSelectionthatmeansyouwillimportdatainthewhole
range,i.e.All7rowsofcolumnsAandB.
(a) Nowsupposewewantedignorethefirstrowaswouldbethecaseifrow1hadlabelsthatare
nonnumeric.ThenwewouldneedtoadjusttherangeaccordinglybyclickingintheRange
windowandchangingrangefromA1:B7toA2:B7.Thiswillresultinrows2to7ofboth
columnsAandBbeinghighlightedinblue.IfyounowclickontheImportDatafromthe
ImportSelectionicon,thisloadthesubsethighlightedinblueasshownbelowinFigure2
3|P a g e
Figure2
ThesubsetdataimportedisgiventwotemporarynamesVarName1andVarName2.Whenyou
wishtodisplaythesetwonewlycreatedarrayswhicharesubsetsofsample3yougetthe
following.
>>[VarName1,VarName2]
ans=
3.080037.6000
5.250046.0000
8.750050.6000
12.300056.1000
16.100059.2000
30.200065.0000
(b) Nowsupposewewantedtousethesubsetwithrow3torow6ofbothcolumns;thenwewould
needtoadjustthedataRangefromA1:B7toA3:B6asshowninFigure3below:
4|P a g e
Figure3
Contentsofthevariablescreatedinthissubsetimportationstepwillbesavedinthetemporaryvariable
namesVarName1,VarName2;thevaluesofwhichareechoedattheMATLABpromptas:
>>[VarName1,VarName2]
ans=
5.250046.0000
8.750050.6000
12.300056.1000
16.100059.2000
Inallcasesinvolvingsubsetsoftheoriginaldataset,itisnowpossibletomanipulatethatdata
asdesiredusingbuiltinoruserdefinedfunctions.
5|P a g e