You are on page 1of 8

9/29/2015

MATLAB Data Import

MATLABDataImport
Advertisements

PreviousPage

NextPage

Importing data in MATLAB means loading data from an external file. The importdata function
allowsloadingvariousdatafilesofdifferentformats.Ithasthefollowingfiveforms
S.N.

FunctionandDescription

A=importdata(filename)
LoadsdataintoarrayAfromthefiledenotedbyfilename.

A=importdata('pastespecial')
Loadsdatafromthesystemclipboardratherthanfromafile.

A=importdata(___,delimiterIn)
InterpretsdelimiterIn as the column separator in ASCII file, filename, or the clipboard
data.YoucanusedelimiterInwithanyoftheinputargumentsintheabovesyntaxes.

A=importdata(___,delimiterIn,headerlinesIn)
Loads data from ASCII file, filename, or the clipboard, reading numeric data starting
fromlineheaderlinesIn+1.

[A,delimiterOut,headerlinesOut]=importdata(___)
ReturnsthedetecteddelimitercharacterfortheinputASCIIfileindelimiterOutandthe
detectednumberofheaderlinesinheaderlinesOut,usinganyoftheinputargumentsin
theprevioussyntaxes.

http://www.tutorialspoint.com/matlab/matlab_data_import.htm

1/8

9/29/2015

MATLAB Data Import

Bydefault,Octavedoesnothavesupportforimportdata()function,soyouwillhave
tosearchandinstallthispackagetomakefollowingexamplesworkwithyour
Octaveinstallation.

Example1
Letusloadanddisplayanimagefile.Createascriptfileandtypethefollowingcodeinit:
filename='smile.jpg'
A=importdata(filename)
image(A)

Whenyourunthefile,MATLABdisplaystheimagefile.However,youmuststoreitinthecurrent
directory.

Example2
Inthisexample,weimportatextfileandspecifyDelimiterandColumnHeader.Letuscreatea
spacedelimitedASCIIfilewithcolumnheaders,namedweeklydata.txt.
Ourtextfileweeklydata.txtlookslikethis
SunDayMonDayTuesDayWednesDayThursDayFriDaySaturDay
95.0176.2161.5440.5755.7970.2881.53
73.1145.6579.1993.5575.2969.8774.68
60.6841.8592.1891.6981.3290.3874.51
48.6082.1473.8241.030.9967.2293.18
89.1344.4757.6389.3613.8919.8846.60

http://www.tutorialspoint.com/matlab/matlab_data_import.htm

2/8

9/29/2015

MATLAB Data Import

Createascriptfileandtypethefollowingcodeinit
filename='weeklydata.txt'
delimiterIn=''
headerlinesIn=1
A=importdata(filename,delimiterIn,headerlinesIn)
%Viewdata
fork=[1:7]
disp(A.colheaders{1,k})
disp(A.data(:,k))
disp('')
end

Whenyourunthefile,itdisplaysthefollowingresult
SunDay
95.0100
73.1100
60.6800
48.6000
89.1300

MonDay
76.2100
45.6500
41.8500
82.1400
44.4700

TuesDay
61.5400
79.1900
92.1800
73.8200
57.6300

WednesDay
40.5700
93.5500
91.6900
41.0300
89.3600

ThursDay
http://www.tutorialspoint.com/matlab/matlab_data_import.htm

3/8

9/29/2015

MATLAB Data Import

55.7900
75.2900
81.3200
0.9900
13.8900

FriDay
70.2800
69.8700
90.3800
67.2200
19.8800

SaturDay
81.5300
74.6800
74.5100
93.1800
46.6000

Example3
Inthisexample,letusimportdatafromclipboard.
Copythefollowinglinestotheclipboard
Mathematicsissimple
Createascriptfileandtypethefollowingcode
A=importdata('pastespecial')

Whenyourunthefile,itdisplaysthefollowingresult
A=
'Mathematicsissimple'

LowLevelFileI/O
Theimportdatafunctionisahighlevelfunction.ThelowlevelfileI/OfunctionsinMATLABallow
the most control over reading or writing data to a file. However, these functions need more
detailedinformationaboutyourfiletoworkefficiently.
http://www.tutorialspoint.com/matlab/matlab_data_import.htm

4/8

9/29/2015

MATLAB Data Import

MATLABprovidesthefollowingfunctionsforreadandwriteoperationsatthebyteorcharacter
level
Function

Description

fclose

Closeoneorallopenfiles

feof

Testforendoffile

ferror

InformationaboutfileI/Oerrors

fgetl

Readlinefromfile,removingnewlinecharacters

fgets

Readlinefromfile,keepingnewlinecharacters

fopen

Openfile,orobtaininformationaboutopenfiles

fprintf

Writedatatotextfile

fread

Readdatafrombinaryfile

frewind

Movefilepositionindicatortobeginningofopenfile

fscanf

Readdatafromtextfile

fseek

Movetospecifiedpositioninfile

ftell

Positioninopenfile

fwrite

Writedatatobinaryfile

ImportTextDataFileswithLowLevelI/O
MATLABprovidesthefollowingfunctionsforlowlevelimportoftextdatafiles
ThefscanffunctionreadsformatteddatainatextorASCIIfile.
Thefgetlandfgetsfunctionsreadonelineofafileatatime,whereanewlinecharacter
separateseachline.
Thefreadfunctionreadsastreamofdataatthebyteorbitlevel.

Example
http://www.tutorialspoint.com/matlab/matlab_data_import.htm

5/8

9/29/2015

MATLAB Data Import

Wehaveatextdatafile'myfile.txt'savedinourworkingdirectory.Thefilestoresrainfalldatafor
threemonthsJune,JulyandAugustfortheyear2012.
The data in myfile.txt contains repeated sets of time, month and rainfall measurements at five
places.TheheaderdatastoresthenumberofmonthsMsowehaveMsetsofmeasurements.
Thefilelookslikethis
RainfallData
Months:June,July,August

M=3
12:00:00
June2012
17.2128.5239.7816.5523.67
19.150.3517.57NaN12.01
17.9228.4917.4017.0611.09
9.599.33NaN0.310.23
10.4613.17NaN14.8919.33
20.9719.5017.6514.4514.00
18.2310.3417.9516.4619.34
09:10:02
July2012
12.7616.9414.3811.8616.89
20.4623.17NaN24.8919.33
30.9749.5047.6524.4534.00
18.2330.3427.9516.4619.34
30.4633.17NaN34.8929.33
30.9749.5047.6524.4534.00
28.6730.3427.9536.4629.34
15:03:40
August2012
17.0916.5519.5917.2519.22
17.5411.4513.4822.5524.01
NaN21.1925.8525.0527.21
26.7924.9812.2316.9918.67
17.5411.4513.4822.5524.01
NaN21.1925.8525.0527.21
26.7924.9812.2316.9918.67

Wewillimportdatafromthisfileanddisplaythisdata.Takethefollowingsteps
Openthefilewithfopenfunctionandgetthefileidentifier.
Describethedatainthefilewithformatspecifiers,suchas'%s'forastring,'%d'foran
integer,or'%f'forafloatingpointnumber.
To skip literal characters in the file, include them in the format description. To skip a
datafield,useanasterisk('*')inthespecifier.
Forexample,toreadtheheadersandreturnthesinglevalueforM,wewrite:
http://www.tutorialspoint.com/matlab/matlab_data_import.htm

6/8

9/29/2015

MATLAB Data Import

M=fscanf(fid,'%*s%*s\n%*s%*s%*s%*s\nM=%d\n\n',1)

By default,fscanf reads data according to our format description until it does not find
any match for the data, or it reaches the end of the file. Here we will use for loop for
reading3setsofdataandeachtime,itwillread7rowsand5columns.
Wewillcreateastructurenamedmydataintheworkspacetostoredatareadfromthe
file.Thisstructurehasthreefieldstime,month,andraindataarray.
Createascriptfileandtypethefollowingcodeinit
filename='/data/myfile.txt'
rows=7
cols=5

%openthefile
fid=fopen(filename)

%readthefileheaders,findM(numberofmonths)
M=fscanf(fid,'%*s%*s\n%*s%*s%*s%*s\nM=%d\n\n',1)

%readeachsetofmeasurements
forn=1:M
mydata(n).time=fscanf(fid,'%s',1)
mydata(n).month=fscanf(fid,'%s',1)

%fscanffillsthearrayincolumnorder,
%sotransposetheresults
mydata(n).raindata=...
fscanf(fid,'%f',[rows,cols])
end
forn=1:M
disp(mydata(n).time),disp(mydata(n).month)
disp(mydata(n).raindata)
end

%closethefile
fclose(fid)

Whenyourunthefile,itdisplaysthefollowingresult
12:00:00
June2012
17.210017.570011.090013.170014.4500
28.5200NaN9.5900NaN14.0000
39.780012.01009.330014.890018.2300
16.550017.9200NaN19.330010.3400
23.670028.49000.310020.970017.9500
19.150017.40000.230019.500016.4600
http://www.tutorialspoint.com/matlab/matlab_data_import.htm

7/8

9/29/2015

MATLAB Data Import

0.350017.060010.460017.650019.3400

09:10:02
July2012
12.7600NaN34.000033.170024.4500
16.940024.890018.2300NaN34.0000
14.380019.330030.340034.890028.6700
11.860030.970027.950029.330030.3400
16.890049.500016.460030.970027.9500
20.460047.650019.340049.500036.4600
23.170024.450030.460047.650029.3400

15:03:40
August2012
17.090013.480027.210011.450025.0500
16.550022.550026.790013.480027.2100
19.590024.010024.980022.550026.7900
17.2500NaN12.230024.010024.9800
19.220021.190016.9900NaN12.2300
17.540025.850018.670021.190016.9900
11.450025.050017.540025.850018.6700

PreviousPage

NextPage
Advertisements

Write for us

FAQ's

Helping

Contact

Copyright 2015. All Rights Reserved.


Enter email for newsletter

http://www.tutorialspoint.com/matlab/matlab_data_import.htm

go

8/8