Lecture23 - Computer Science

advertisement

Lecture 22:

Reading and writing text files gerasoulis 1

Overview

• Reading Data from a file.

• Reading fixed format table-like data.

• Writing Data to a string

• Writing data to a file gerasoulis 2

Reading a text file into a cell array

• Large amounts of text data can be stored in

“plain text” or “plain ascii” files, with a .txt

extension, e.g. mobydick.txt

• ========mobydick.txt=======

Call me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore,

I thought I would sail about a little and see the watery part of the world.

It is a way I have of driving off the spleen and regulating the circulation.

gerasoulis 3

Reading text files

• lines = textread( 'mobydick.txt' , '%s' ,

'delimiter' , '\n' );

• Successive lines of a text file can be read into the cells of a cell array using the textread function.

• '%s' , = reading a string ,

• 'delimiter' , '\n'= reading until the end of line.

gerasoulis 4

Cell array

• Textread creates a cell array or an array

• In the previous example lines is a cell array.

• lines{3,1}=

I thought I would sail about a little and see the watery part of the world.

>> size(lines)

• ans =

6 1 gerasoulis 5

Changing Delimiter

• Delimiter could be a period, a comma a letter etc.

• What will the following do?

• lines = textread( 'mobydick.txt' , '%s' ,

'delimiter' , '.' );

• lines = textread( 'mobydick.txt' , '%s' ,

'delimiter' , ' ' ); gerasoulis 6

Reading formatted data

• Suppose we have a text file elements.txt that contains a combination of numerical data and strings, in a fixed format.

• 1

• 2

• 3

• 4

• 5

• 6

• 7

• 8 hydrogen helium lithium beryllium boron carbon nitrogen oxygen

• 9 fluorine

• 10 neon

H 1.01

He 4.00

Li 6.94

Be 9.01

B 10.81

C 12.01

N 14.01

O

F

16.00

19.00

Ne 20.18

gerasoulis 7

Elements.txt

The first column is integers, second strings, third strings and fourth real(float).

• format :

• %u integer

• %s string

• %f float

• textread( 'elements.txt' , ʻ %u %s %s %f ' ); gerasoulis 8

Elements.txt

• We can read each column into a vectors of same type elements.

• [atomNums names symbols masses] = ...

textread( 'elements.txt' , '%u %s %s %f' );

• See readTable.m

• Tab={atomNums names symbols masses}

Will generate a cell array Tab of the table that can be passed as a single argument.

See byName.m

gerasoulis 9

atomNums names symbols masses atomNums =

1

2

3

4

5

6

7

8

9

10 names =

'hydrogen'

'helium'

'lithium'

'beryllium'

'boron'

'carbon'

'nitrogen'

'oxygen'

'fluorine'

'neon' symbols =

'H'

'He'

'Li'

'Be'

'B'

'C'

'N'

'O'

'F'

'Ne' masses =

1.0100

4.0000

6.9400

9.0100

10.8100

12.0100

14.0100

16.0000

19.0000

20.1800

gerasoulis 10

For more textread details:

• >>>help textread

• TEXTREAD Read formatted data from text file.

• A = TEXTREAD('FILENAME')

• A = TEXTREAD('FILENAME','',N)

• A =

TEXTREAD('FILENAME','',param,value,

...) gerasoulis 11

For more textread details:

• Supported conversion specifications:

• %n - read a number - float or integer (returns double array)

• %5n reads up to 5 digits or until next delimiter

• %d - read a signed integer value (returns double array)

• %5d reads up to 5 digits or until next delimiter

• %u - read an integer value (returns double array)

• %5u reads up to 5 digits or until next delimiter

• %f - read a floating point value (returns double array)

• %5f reads up to 5 digits or until next delimiter

• %s - read a whitespace separated string (returns cellstr)

• %5s reads up to 5 characters or until whitespace

• %q - read a double-quoted string, ignoring the quotes (returns cellstr)

• %5q reads up to 5 non-quote characters or until whitespace

• %c - read character or whitespace (returns char array)

• %5c reads up to 5 characters including whitespace

• ….

gerasoulis 12

Printing data to A STRINGsprintf

• STR = SPRINTF(FORMAT, A, ...)

• applies the FORMAT to all elements of array A and any additional array arguments in column order, and returns the results to string STR.

• str=sprintf('%15.5f',1/eps) str= 4503599627370496.00000

• sprintf('%s','hello')

hello gerasoulis 13

sprintf

• A=[1 2 3]

• >> sprintf(' %f ',A)

• 1.000000 2.000000 3.000000

• Notice the blank spaces in the format.

• Without blank spaces it will print as follows

• >> sprintf('%f',A)

1.0000002.0000003.000000

gerasoulis 14

sprintf

• sprintf(' %u ',A)

• ans =

1 2 3

• sprintf('%d',A)

• ans =

123

• sprintf( '%5d',A) ==?????

gerasoulis 15

Printing column vectors

• Assume now that we have a column vector—Notice that sprintf prints the elements as row vector--PRINTING S A

STRING a ROW-WISE OPERATION !

• B=[1

2

3] sprintf('%5d',B) ans =

1 2 3 gerasoulis 16

Sprintf

• If we want to use sprintf to print elements column-wise then we need to print each element at a time!

• sprintf('%5d',B(1))

• ans =

1

• >> sprintf('%5d',B(2))

• ans =

2 gerasoulis 17

Printing Column-wise with sprintf

• >> for i=1:3 st(i,:)=sprintf('%5d',B(i)); end

• st =

1

2

3 gerasoulis 18

Writing on files

• Instead of writing the results on a variable or on the matlab screen we can also

WRITE the results on file. We can do this as follows:

• First open a file.txt

• fid = fopen( ʻ file.txt', 'w');

• fid====File ID

• w===Write

• file.txt===File name gerasoulis 19

Writing on files-fprintf

• fprintf(fid,'%5d',A)

• This command will write on the file with fid the vector A row-wise.

File.txt 1 2 3

• To STOP writing on this file you must

CLOSE it using

• fclose(fid)

See printTable.m

gerasoulis 20

Example with \n = next line gerasoulis 21

Example with \n = next line

• fid = fopen('results.txt', 'w');

• fprintf(fid, 'experimental results:');

• fprintf(fid, '\nxdata: ');

• fprintf(fid, '%6.2f', xdata);

• fprintf(fid, '\nydata: ');

• fprintf(fid, '%6.2f', ydata);

• fprintf(fid, '\nresults: ');

• fprintf(fid, '%6.2f', results);

• fclose(fid); gerasoulis 22

Writing files with literal strings gerasoulis 23

Writing files with literal strings gerasoulis 24

Writing files with literal strings

• fid = fopen('cs112.txt', 'w');

• fprintf(fid, 'data for CS112 assignment work

\nSpring 2007 \n\n');

• for i = 1:length(data)

• fprintf(fid, '%s\nused drop-in %u times\naverage assignment time %3.1f

hours\nassignment grade %4.1f\n\n', data{i}{1}, data{i}{2}, data{i}{3}, data{i}{4});

• end

• fclose(fid); gerasoulis 25

Reading an Excel File-xlsread

• We can also read excel spreadsheet into

Matlab cell arrays.

• [NUMERIC,TXT,RAW]=XLSREAD(FILE)

• NUMERIC= is the numerical part of the excel file.

• TXT= is the text part

• RAW= is the entire excel file in RAW format gerasoulis 26

gerasoulis 27

[NUMERIC,TXT,RAW]=XLSRE

AD('Book1.xlsx')

• TXT =

TXT

• Columns 1 through 5

• 'Date' 'Time' 'Open' 'High' 'Low'

• '10/24/2005' '' '' '' ''

• '10/25/2005' '' '' '' ''

• '10/26/2005' '' '' '' ''

• '10/27/2005' '' '' '' ''

• '10/28/2005' '' '' '' ''

• '10/31/2005' '' '' '' ''

• '11/1/2005' '' '' '' ''

• '11/2/2005' '' '' '' ''

• '11/3/2005' '' '' '' ''

• '11/4/2005' '' '' '' ''

• '11/7/2005' '' '' '' ''

• '11/8/2005' '' '' '' '' gerasoulis 28

RAW

• RAW =

• Columns 1 through 4

• 'Date' 'Time' 'Open' 'High'

• '10/24/2005' [0.3958] [55.2500] [56.7900]

• '10/25/2005' [0.3958] [56.4000] [56.8500]

• '10/26/2005' [0.3958] [56.2800] [57.5600]

• '10/27/2005' [0.3958] [56.9900] [57.0100]

• '10/28/2005' [0.3958] [56.0400] [56.4300]

• '10/31/2005' [0.3958] [55.2000] [57.9800]

• '11/1/2005' [0.3958] [57.2400] [58.1400]

• '11/2/2005' [0.3958] [57.7200] [ 60]

• '11/3/2005' [0.3958] [60.2600] [62.3200]

• '11/4/2005' [0.3958] [60.3500] [61.2400]

• '11/7/2005' [0.3958] [60.8500] [61.6700]

• '11/8/2005' [0.3958] [59.9500] [60.3800

]

29

RAW

• Columns 5 through 7

• 'Low' 'Close' 'Volume'

• [55.0900] [56.7900] [21884143]

• [55.6900] [56.1000] [16655439]

• [55.9200] [57.0300] [22608343]

• [55.4100] [55.4100] [15044140]

• [54.1700] [54.4700] [27503152]

• [54.7500] [57.5900] [33727255]

• [56.8700] [57.5000] [26837071]

• [57.6000] [59.9500] [30857901]

• [60.0700] [61.8499] [31585059]

• [59.6200] [61.1500] [31368909]

• [60.1400] [60.2300] [22815383]

• [59.1000] [59.9000] [16920183] gerasoulis 30

Download