Title: Matlab Training Session 5: Importing Data
1Matlab Training Session 5Importing Data
2- Course Outline
- Weeks
- Introduction to Matlab and its Interface (Sept.
27 2005) - Fundamentals (Operators)
- Fundamentals (Flow)
- Importing Data
- Functions and M-Files
- Plotting (2D and 3D)
- Statistical Tools in Matlab
- Analysis and Data Structures
- Classes on ? Nov 1st ?, 8th and 15th have been
cancelled for Society for Neuroscience (SFN) 35th
annual meeting. - Course Website
- http//www.queensu.ca/neurosci/Matlab Training
Sessions.htm
3- Week 5 Lecture Outline
- Importing Data
- A. Week 4 Review
- B. Simple Import
- C. Handling Files
- D. Mini-Project
4Functions in Matlab
- In Matlab, each function is a .m file
- It is good protocol to name your .m file the same
as your function name, i.e. funcname.m - function outargsfuncname(inargs)
Function
input
output
5Mini-Project
- Raising any number of numbers to the nth power
- Inputs
- A vector of numbers to be raised (N1Nm)
- A vector of powers (P1Pm)
- Outputs
- A vector of raised values (N1P1 NmPm)
- An error flag 1 if error in calculation, 0 if
successful - Caveats
- If only one input is provided, the function
should square each entry, so output (N12Nm2)
and error flag is 0 - If the length of N and P are not the same, this
is an error, return anything in the output vector
and a 1 in the error flag - Make sure to comment and document the function
6Solution
Complex function y, e raise(x,n) y
ones(1,length(x)) if nargin 1 y e
raise(x,2ones(1,length(x))) return elseif
nargin 2 if(length(x)length(n))
y NaN e 1 return end
for(i1length(x)) for(j1n(i))
y(i) y(i)x(i) end end e
0 return end
Simple function y, e raise(x,n) if nargin
1 y e x.2 return elseif nargin
2 if(length(x)length(n)) y NaN
e 1 return end y
x.n e 0 end
7Importing Data
- Basic issue
- How do we get data from other sources into Matlab
so that we can play with it? - Other Issues
- Where do we get the data?
- What types of data can we import
- Easily or Not
8load
- 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
9load
- 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 (ie.
where the file is) and type load(test1.txt) - Now, type x load(test1.txt)
1 2 3 4 5 2 4 8 16 32
test1
10load
- 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 and
text2.txt - type y load(test2.txt)
- Error!
1 2 3 4 5 4 8 16 32
test2
11load
- 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?
- Forcing the issue with Excel data
test1
1 2 3 4 5 2 4 8 16 32
12load
- 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
13File 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
14fopen
- 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.da
t - 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
15fclose
- When you are done with a file, it is a good idea
to close it especially if you are opening many
files - fclose(fid)
16What 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
file_name
Pointer
17Moving 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)
- Move pointer in file fid by offset relative to
origin - Origin can be beginning, current, end of file
- To point to the beginning
- frewind(fid)
18Getting 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, f, c
- Size
- You can specify how to organize the imported data
- m,n import the data as m by n, n can be
infinite - Be careful because matlab will mangle your data
and not tell you
19Lets Try It
- Open text1.txt using the fopen command
- Remember to save the fid, we will need it
- Create a variable with the data of text1.txt
- Now create another variable y with the data of
text1.txt in it by using fscanf (do not simply
copy x) - What happens here?
- Need to set file pointer to beginning using
rewind(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. This is
powerful but dangerous
20Getting Data
- fgetl returns the next line of the file as a
character array - You may need to convert these to numbers
gtgt fid1 fopen(test1.txt) gtgt a_str
fgetl(fid1) a_str 1 2 gtgt a_num
str2num(a_str) a_num 1 2
21Realistic File
- A realistic file of data will have header
information, labeled columns and other
information embedded within it. - See PDXtemp.dat
- Option 1 Manually go through deleting this
information and import using load of fopen
commands. - Option 2 Have matlab delete and format available
data on the fly
22Realistic File
- Powerful function textread can be used to input
almost any text file - Handles
- Opening the file
- Ignoring Header Information
- Accepting Column Labels
- Will work for most applications
23Summary
- Lots of options to load files
- load for basics
- fscanf for complex
- textread for most things
- xlsread for Excel worksheets
- Also saving Excel sheets as tad delimitted
24Mini-Project
- Using the textread function, import the full data
located in PDXtemp.dat with the stated names and
correct data types - Take the resulting temperatures in Fahrenheit and
convert to Celsius - Make use of Matlab help to learn about and
implement the textread function - Deg_F 9/5Deg_C 32
25Getting Help
- Help and Documentation
- Digital
- Accessible Help from the Matlab Start Menu
- Updated online help from the Matlab Mathworks
website - http//www.mathworks.com/access/helpdesk/help/tech
doc/matlab.html - Matlab command prompt function lookup
- Built in Demos
- Websites
- Hard Copy
- Books, Guides, Reference
- The Student Edition of Matlab pub. Mathworks Inc.