+ All Categories
Home > Documents > Early File I/O To help you get started with your final project 1. Definition of “high level” 2....

Early File I/O To help you get started with your final project 1. Definition of “high level” 2....

Date post: 08-Jan-2018
Category:
Upload: barnard-snow
View: 218 times
Download: 0 times
Share this document with a friend
Description:
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
29
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread() 4. xlswrite() 5. dlmread() 6. dlmwrite() 1
Transcript
Page 1: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Early File I/OTo help you get started with your final

project

1. Definition of “high level”2. Is using a High Level function appropriate?3. xlsread()4. xlswrite()5. dlmread()6. dlmwrite()

1

Page 2: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

1. Definition A file I/O function is considered HIGH-LEVEL if in ONE

SINGLE COMMAND, it 1. Opens the file2. Grabs the data3. Stores it in variables4. 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

Page 3: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

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

Page 4: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

2. Can High Level be used?

4

Page 5: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

2. Can High Level be used? cont.

“Neatly Organized”

1. Rows and Columns are “identifiable”

2. No “holes” anywhere3. Always the same patterns per

line4. There is no mixes of delimiters

(commas, spaces, tabs, dash.. )

5

Page 6: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Questions Can these files be read using a high level function?

6

Page 7: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Answers Can these files be read using a high level function?

7

Yes Mixes of – and spaces.

Mixes of , and spaces. Yes. White space only.Decimals are ok.

Page 8: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

The input files (.txt, .xls, .xlsx, .m) should all be in the same directory for function calls to work!

8

Page 9: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

2. Can High-Level be used?

9

Page 10: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

3. xlsread() F1, help. The most general form is:

Other calls possible:

10

Page 11: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

3. xlsread() F1, help. The most general form is:

Other calls possible:

11How do we work with all this?

Page 12: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Example: xlsread() Reading from a grade book %load data from file[values text raw] = xlsread('grades.xlsx');

12

Test. See results!

Page 13: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Example: xlsread() Reading from a grade book %load data from file[values text raw] = xlsread('grades.xlsx');

13

Test. See results!

Note that we are collecting multiple values using an array!

Page 14: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

Results

14

Page 15: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

3. xlsread(), cont.

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

15

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.

Page 16: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

3. xlsread(), cont. Simply omit the 2nd and 3rd return value to collect only

numerical values.values = xlsread(‘grades.xlsx’);

16

Page 17: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

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

17

Page 18: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

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 happen 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

Page 19: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

3. xlsread(), cont.

This function will not work under Citrix, since Excel is not installed on Citrix. Come to the lab to do labs and homework.

19

Page 20: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

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)

Page 21: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

2. Can High-Level be used?

21

NUMBERS ONLY.

Page 22: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

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

22

Page 23: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

5. Using Delimiters, cont. Delimiter: white space (spaces, or tabs)

23Missing data is filled by zeros.

Page 24: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

5. Using Delimiters, cont. Delimiter: commas Added feasibility to ‘skip’ columns

24

Page 25: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

5. Using Delimiters, cont. Delimiter: other than the defaults

25

Specify the delimiter as the 2nd argument.

Page 26: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

5. Using Delimiters, cont. Delimiter: other than the defaults

26

Specify the delimiter as the 2nd argument.

Page 27: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

5. Using Delimiters, cont. "Neatly Organized" is important.

27

BAD RESULTS.Two delimiters (spaces and colons) in the file. Matlab is lost.

Page 28: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

dlmwrite()M = [2, 123; 87, 4];dlmwrite('test.txt', M, ',');

File contains:2,12387,4

EXCEPT…

28

Page 29: Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()

dlmwrite()By default, dlmwrite() saves files using Unix line endings – so if

you look at the file in Notepad:

This is not “wrong” – just different. This can be changed by setting the “newline” attribute when you call the function:

dlmwrite('test.txt', M, ',', 'newline', 'pc');

29


Recommended