Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

IEA 216/3: Computer

Applications in Industry (Matlab)


Importing Data
Importing Data
• Basic issue:
– How do we get data from other sources into
Matlab?
• Other Issues:
– Where do we get the data?
– What types of data can we import?
• Easy or difficult
load
• Command opens and imports data from a
standard ASCII file into a Matlab variable
• Restrictions
– Data must be constantly sized
– Data must be ASCII
– No other characters
load
• Consider the simple file below
• Create in notepad and save as test1.txt and
text2.txt
• In Matlab, set the path to the correct place (i.e..
where the file is) and type load(‘test1.txt’)
• Now, type x = load(‘test1.txt’)
12
34
52
48
16 32
load
• Now the file is no longer simple because not
every row has the same amount of characters
• Create in notepad and save as test2.txt
• type y = load(‘test2.txt’)
– Error!

12
34
5
48
16 32
load
• Now type in the same thing from test1.txt into
Excel and save the workbook as test1.xls
• type y = load(‘test1.xls’)
– What happens?
• Matlab would display an error
• Forcing the issue with Excel data by changing to
text file and then import. 12
34
52
48
16 32
load
• Works for simple and unstructured code
• Powerful and easy to use but limited
• Will likely force you to manually handle
simplifying data which is prone to error
• More complex functions are more flexible
File Handling
• f* functions are associated with file opening,
reading, manipulating, writing, …
• Basic Functions of Interest for opening and
reading generic files in Matlab
– fopen
– fclose
– fseek/ftell/frewind
– fscanf
– fgetl
fopen
• Opens a file object in Matlab that points to the
file of interest
• fid = fopen(‘filepath’)
– absolute directory + filename
• If file of interest it C:\Andrew\Project_1\file.dat
• >> fid = fopen(‘C:\Andrew\Project_1\file.dat’)
– relative path + filename
• If your Matlab path is set to c:\Andrew\Project_1
• >> fid = fopen(‘file.dat’)
• fid is an integer that represents the file
– Can open multiple files and Matlab will assign unique
fids
fclose
• When you are done with a file, it is a good idea
to close it especially if you are opening many
files
– >> fclose(fid)
What is a File?
• A specific organization of data
• In Matlab it is identified with a fid
• Location is specified with a pointer that can be
moved around
fid

Pointer

file_name
Moving the Pointer
• We already know how to assign a fid (fopen)
• To find where the file is pointing:
– >> x = ftell(fid)
• To point somewhere else
– >> fseek(fid,offset,origin)
– E.g., fseek(fid,10,0) %no need for an output
• Move pointer in file fid by offset relative to origin
– Origin can be beginning, current, end of file
– Offset is in bytes
– Output is ans = 0

• To point to the beginning


– frewind(fid)
– E.g., frewind(fid); %no output
Getting Data
• Why move the pointer around?
– Get somewhere in the file from where you want data
• fscanf(fid,format,size)
• Format
– You have to tell Matlab the type of data it should be
expecting in the text file so that it can convert it
• ‘%d’(integers), ‘%f’ (decimal numbers), ‘%c’(single
character)
• Data must be of the same data type specified.
• Size
– You can specify how to organize the imported data
• [m,n] – import the data as m by n
• Be careful because Matlab will mangle your data and not tell
you
• >> A = fscanf(fid,‘%d’,[5,2]);
Lets Try It
• Open test1.txt using the fopen command
– Remember to save the fid, we will need it
• Create a variable with the data of test1.txt using fscanf() without
specifying dimensions – a vector data is created.
• Now create another variable y with the data of test1.txt in it by using
fscanf (do not simply copy x)
– What happens here? No data is loaded into x.
– Need to set file pointer to beginning using frewind(fid)
• Now use the size option to import the data with 5 rows and 2
columns
• Try the same thing with test2.txt
– It works and fills in the blanks where the last element is 0. This is
powerful but dangerous because missing data would just be ignored.
Getting Data
• fgetl returns the next line of the file as a
character array, line by line.
• You may need to convert these to numbers.

>> fid1 = fopen(‘test1.txt’);


>> a_str = fgetl(fid1)
a_str = 1 2
>> a_num = str2num(a_str)
a_num = [1 2]
Realistic File
• A realistic file of data will have header information,
labeled columns and other information embedded within
it.
– See pm10_data.txt (in elearning.usm.my)
• Option 1: Manually go through deleting this information
and import using load or fopen commands.
• Option 2: Have Matlab delete and format available data
on the fly by creating a custom script (.m file).
Realistic File
• Powerful function textread can be used to
input almost any text file
• Handles:
– Opening the file
– Ignoring Header Information (‘headerlines’)
• Will work for most applications
• >> [USM,S18] = textread(‘pm10_data.txt’,’%f%f’,’headerlines,1);
– Create 2 variables “USM” and “S18” with their corresponding data from
pm10_data.txt.
Summary
• Lots of options to load files
– load for basics
– fscanf for complex
– textread for most things
– xlsread for Excel worksheets
• >> test1_excel = xlsread(‘test1.xls’)
• Also saving Excel sheets as “tab” delimited

You might also like