22
Matlab DIY Lesson 1: Reading Data

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

Matlab DIY

Lesson 1: Reading Data

Page 2: 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

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

Page 3: 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

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!

Page 4: 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

Course Materials

Course Website:http://indra.uoregon.edu/~admin/mediawiki-1.13.4/index.php/Matlab_DIY_for_Beginners

Page 5: 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

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

Page 6: 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

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

Page 7: 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

Data encoded in a CSV

 

Trial,RT,Response,Expected1,423,1,12,547,2,13,579,2,24,324,2,15,446,1,1

Page 8: 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

Data Encoded in a TSV

Trial    RT    Response    Expected1    423    1    12    547    2    13    579    2    24    324    2    15    446    1    1

Page 9: 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

An example of a Data in a .txt File

commas and white space were used to separate the rows and columns

Page 10: 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

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?

    

Page 11: 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

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

    

Page 12: 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

Workspace can help you identify what Matlab sees

These all look like 0 to us but Matlab sees them differently

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

Data type determines how Matlab reads data

subj_01, male, 34, right

54301, 4578, 44478, 234

csvread

textscan

23    45    67    89 dlmread

Page 17: 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

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;

Page 18: 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

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/

Page 19: 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

Declaring File Name and Path

%Name of the Filefilename = 'L1_data.csv'; %Path to File filepath = 'C:\Desktop\Classwork\' ;

Page 20: 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

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');

Page 21: 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

 

Questions?

Page 22: 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

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.