file i/o high-level functions 1. definition 2. is a high-level function appropriate? 3. xlsread() 4....

28
File I/O High-Level Functions 1. Definition 2. Is a High-Level function appropriate? 3. xlsread() 4. dlmread() 1

Upload: noel-hensley

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

1. Definition

A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it 1. Opens the file

2. Grabs the data

3. Stores it in variables

4. Closes the file

High-level functions can be used to:1. Load data from external sources (INPUT)

2. Save data back to a file (OUTPUT)

2

2. Wait.wait.wait…

Before you decide if using a high-level function is possible, evaluate the following:

1. What type of file is it? (excel, text, jpg..)

2. Find the organization overall (data in rows, or data in columns, data placed all over the place…)

3. Recognize the delimiters (space, tabs, new lines, -, :, / any specific symbol. What makes it obvious it is a new column? What makes it obvious it is a new row?)

4. Recognize the data types (all numerical? all strings? Or combination of both)

3

Example: xlsread()

Name who has the highest grade! %load data from file[values text raw] = xlsread('grades.xlsx');

%find the position of the maximum grade[trash row]= max(values(:,2));

%find whose name is associated with that positionname = text{row+1,1}; %+1 due to headersfprintf('%s got the highest grade\n', name)

15

Example: xlsread()

Name who has the highest grade! %load data from file[values text raw] = xlsread('grades.xlsx');

%find the position of the maximum grade[trash row]= max(values(:,2));

%find whose name is associated with that positionname = text{row+1,1}; %+1 due to headersfprintf('%s got the highest grade\n', name)

16

3. xlsread(), cont.

txt = 19 78 22 83 98 99 21 56 23 89 19 51

17

nbs = 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy' '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' ''

raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

>> [txt nbs raw] = xlsread('grades.xlsx‘)

Variable names are up to the programmer. If named badly by mistake… BAD.

The order of the return values is important.

3. xlsread(), cont.

Simply omit the 2nd and 3rd return value to collect only numerical values.values = xlsread(‘grades.xlsx’);

If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable.[trash trash data] = xlsread(‘grades.xlsx’);

If there happens to be ‘holes’ in the spreadsheet, Matlab fills it with a NaN value (not a number). The function isnan() can help determine where those ‘holes’ are.

18

4. xlswrite()

Arrays can also be written to excel sheets:

xlswrite(<filename>, <array>, <sheet>, <range>)

The <sheet> and <range> arguments are optional.

20

clcclear %create phony datatable = rand(5,3)*100; %print to excelxlswrite('testingTesting.xls',table)

22

5. Using Delimiters

Rows are delimited by the new line character (enter key, invisible to the human eye).

Columns are delimited by the same delimiter each time: Default delimiters: commas, and white space Other delimiters: any other symbol : - /

F1 = help

Wrapping Up

One command opens, loads, and closes the file.

Before anything, look in the file, and decide whether a high-level function is appropriate

xlsread(), dlmread() are common. There are many other high-level functions.

The variables are automatically created by MATLAB. Sometimes as regular array [ ] Other times as cell array { }

Use knowledge from previous chapter to analyze the data in the arrays. FOR loops, A(position), A(row,col), A{row,col},

max(arrayName), mean(arrayName), sort(..), slicing, deleting, augmenting…

28