View
213
Download
0
Embed Size (px)
Citation preview
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 OutLesson 3: "parsing", "batching", other manipulations of dataLesson 4: Formulas (your own and others)Lesson 5: Formulas again! Lesson 6: Plots, the basicsLesson 7:Plots, fun play with plotsLesson 8:Plots again! (graphics for publication quality)Lesson 9: Output again!
Course Materials
Course Website:http://indra.uoregon.edu/~admin/mediawiki-1.13.4/index.php/Matlab_DIY_for_Beginners
Today's Lesson
• What does data look like?• What should we be looking for?
o Organizationo 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,Expected1,423,1,12,547,2,13,579,2,24,324,2,15,446,1,1
Data Encoded in a TSV
Trial RT Response Expected1 423 1 12 547 2 13 579 2 24 324 2 15 446 1 1
An example of a Data in a .txt File
commas and white space were used to separate the rows and columns
Data OrganizationHow is the data presented in the file?• headers?
• data separators?o white spaceo commao 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? Characters (letter) Strings (sequences of letters)
o mixture? Characters (letter or number) Strings (sequences of letters and numbers)
o booleans? 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 programo Matlab's Contiguous Memory Need
Arrays and Cells
1. In Matlab, everything is an Array2. An Array can only store one type of data 3. Cells hold other types4. You can make an Array of Cells5. 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
subj_01, male, 34, right
54301, 4578, 44478, 234
csvread
textscan
23 45 67 89 dlmread
First Step - Writing your first code
Using the Matlab Editor
Comments %This is a comment This is code Testing code in the Matlab Command WindowDeclaring 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 Filefilename = 'L1_data.csv'; %Path to File filepath = 'C:\Desktop\Classwork\' ;
Opening that file
%Name of the Filefilename = 'L1_data.csv'; %Path to File filepath = 'C:\Desktop\Classwork\' ; %Open the file for editingdatafile = 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.