Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Loading Files MATLAB

1. The document describes three options for loading data files into MATLAB: directly loading the entire file, loading and assigning variable names, and loading a subset of the data. 2. It provides an example of directly loading sample3.dat, then computing statistics like mean and standard deviation of the loaded data. 3. Another example loads sample3.dat and assigns the data to variable D, then assigns columns to variables x and y for separate analysis and plotting. 4. The third option involves opening the file, selecting a range to import only a subset, which are given temporary variable names for manipulation.

Uploaded by

emongoloide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Loading Files MATLAB

1. The document describes three options for loading data files into MATLAB: directly loading the entire file, loading and assigning variable names, and loading a subset of the data. 2. It provides an example of directly loading sample3.dat, then computing statistics like mean and standard deviation of the loaded data. 3. Another example loads sample3.dat and assigns the data to variable D, then assigns columns to variables x and y for separate analysis and plotting. 4. The third option involves opening the file, selecting a range to import only a subset, which are given temporary variable names for manipulation.

Uploaded by

emongoloide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

IllustrationoftheloadfacilityinMATLAB

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

%Example loading and naming a data file. For brevity


%name the data set D
D = load('sample3.dat');
%Assign variable name x to the first column; and
%variable y to the second column
x = D(:,1);
y = D(:,2);
meanx = mean(x)
stdevx= std(x)
rootx = sqrt(x)
meany = mean(y)
stdevy= std(y)
rooty = sqrt(y)
plot(x,y,'*')
xlabel('x'), ylabel('y')
grid
Wewilluseasimilarscriptforfittinglinesandcurvesthroughdatalaterintheclass.

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

You might also like