Matlab_L1

advertisement
Matlab DIY
Lesson 1: Reading Data
Purpose of this Seminar
Basic Ability to handle Data Analysis and Presentation in
Matlab
• Understand how data is organized
• Know how to read data into Matlab
• Know how to describe what you want to do with the data
• Know how to use Matlab to transform data into the form
you want it in
• Know how to examine the output for accuracy
Syllabus
Lesson 1: Reading Data In
Lesson 2: Getting Data Out
Lesson 3: "parsing", "batching", other manipulations of data
Lesson 4: Formulas (your own and others)
Lesson 5: Formulas again!
Lesson 6: Plots, the basics
Lesson 7:Plots, fun play with plots
Lesson 8:Plots again! (graphics for publication quality)
Lesson 9: Output again!
Course Materials
Course Website:
http://indra.uoregon.edu/~admin/mediawiki1.13.4/index.php/Matlab_DIY_for_Beginners
Today's Lesson
• What does data look like?
• What should we be looking for?
o Organization
o Data Types
• How do we use that information?
o Memory Requirements for Data
o Picking the right data reading function
What does Data Look Like?
What you can expect:
• .csv -->Comma Separated Values
• .tsv --> Tab Separated Values
• .txt --> Text data in some format
Unfamiliar extension?
• Check the documentation
• http://filext.com/ --> File Extension Search
Data encoded in a CSV
Trial,RT,Response,Expected
1,423,1,1
2,547,2,1
3,579,2,2
4,324,2,1
5,446,1,1
Data Encoded in a TSV
Trial RT Response
1 423 1 1
2 547 2 1
3 579 2 2
4 324 2 1
5 446 1 1
Expected
An example of a Data in a .txt File
commas and white space were used to separate the rows and columns
Data Organization
How is the data presented in the file?
• headers?
• data separators?
o white space
o comma
o tab
• data types?
o numbers?
o letters?
o mixture?
How does Matlab see data?
• data types?
o Numbers?
 Integers (whole numbers)
 Doubles (decimals included)
o
letters?
o
mixture?
o
booleans?
 Characters (letter)
 Strings (sequences of letters)
 Characters (letter or number)
 Strings (sequences of letters and numbers)
 True = 1
 False = 0
Workspace can help you identify what Matlab sees
These all look like 0 to us but Matlab sees them differently
Memory Requirements for Data
How much memory do particular types require?
• Integers - 1,2,4, or 8 bytes (Usually 4)
• Doubles - 8 bytes
• Strings - 2 bytes per character
• Booleans - 8 bytes
Why do we care?
• Program Efficiency
• Memory Limitations within Matlab
o Limited Memory for the program
o Matlab's Contiguous Memory Need
Arrays and Cells
1. In Matlab, everything is an Array
2. An Array can only store one type of data
3. Cells hold other types
4. You can make an Array of Cells
5. A Matrix is an Array of Arrays
6. A Matrix must always be square
How to use what we've learned?
Matlab has multiple data parsing functions.
• The one to use depends on data organization.
Aside from headers, is there non-numerical data?
• Non-Numerical data requires textscan
Do you want all the data in the file?
• Advanced arguments to the function
The above points will be explored in Lesson 3
Data type determines how Matlab reads data
54301, 4578,
44478, 234
23 45
7 89
6
csvread
dlmread
textscan
subj_01,
male, 34,
right
First Step - Writing your first code
Using the Matlab Editor
Comments
%This is a comment
This is code
Testing code in the Matlab Command Window
Declaring a variable
x = 1;
Opening a file from Matlab
What you need:
Name of the File
Path to the File
Filename:
data.csv, collection.txt, subject117.tsv
File Path:
On Windows: C:\My Documents\Collection\
On Mac/Unix: /Users/default/Collection/
Declaring File Name and Path
%Name of the File
filename = 'L1_data.csv';
%Path to File
filepath = 'C:\Desktop\Classwork\' ;
Opening that file
%Name of the File
filename = 'L1_data.csv';
%Path to File
filepath = 'C:\Desktop\Classwork\' ;
%Open the file for editing
datafile = fopen([filename,filepath],'r');
Questions?
Exercises
1) Write a matlab command to open one of your own data
files
2) Use Matlab help. Search for “file formats” to see what
type of files Matlab can read and what the command is for
reading them.
For extra experience:
1 -- Read up on the uigetfile command in Matlab help.
2 -- Figure out how to use this function to get the
path
and name of a file.
Download