formatted input/output operations - marmara...

36
CSE 123 Formatted Input/Output Operations

Upload: others

Post on 21-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

CSE 123

Formatted Input/Output

Operations

Page 2: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Data Import-Export

Why is it important?

Lab experiment results are:

usually recorded in lab (even on paper)

Put together and stored into a data file

Analyzed using mathematical tools

Text file (.txt)

Microsoft Excel file (.xls)

Binary file

What do we want to export?

Save everything in the workspace for post analysis.

Save a selected number of results from the analysis in a text file

(formatted or not)

Page 3: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

fpintf function writes formatted data in a user specified format to a file

fprintf(fid,format,val1,val2, ….)

fid :the file id to which data will be written (no fid for printing on CW)

format :the format string. % always marks the beginning of a format

The structure of a format specifier

%-12.5e

Marker

(Required)

Field Width

(Optional)

Precision

(Optional)

Format Descriptor

(Required)

Modifier

(Optional)

Format effects only the display of variables not their values through program execution

fprintf Function

Page 4: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Specifier Description

%c Single character

%d Decimal notation (signed)

%e Exponential notation

%E Exponential notation

%f Fixed-point notation

%g The more compact of %e and %f. Insignificant zeros do not print

%s String of characters

%u Decimal notation unsigned

Common format specifier notation for fprintf

Page 5: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

symbol Description

\n New line

\t Horizontal tab

\b backspace

\\b Print an ordinary backslash (\)

\‘’ or ‘’ Print an aposthrophe or single quote

%% Print an ordinary percent symbol (%)

Escape characters in Format strings

Page 6: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

fprintf(‘%d\n’,123) ----|----|

123

fprintf(‘%6d\n’,123) ----|----|

123

fprintf(‘%6.4d\n’,123) ----|----|

0123

Example: Decimal (integer) data is displayed using %d format specifier.

If a non decimal number is displayed with the %d specifier, the specifier

will be ignored and the number will be displayed in exponential format

fprintf(‘%6d\n’,123.4) produces 1.234000e+002

Page 7: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

fprintf(‘%f\n’,123.4) ----|----|

123.400000

(default is 6 chars after the decimal place)

fprintf(‘%8.2f\n’,123.4) ----|----|

123.40

fprintf(‘%4.2f\n’,123.4) ----|----|

123.40 (6 chars wide field)

fprintf(‘%10.2e\n’,123.4) ----|----|

1.23e+002

fprintf(‘%10.2E\n’,123.4) ----|----|

1.23E+002

fprintf('%8.4f\n',123.4) ----|----|

123.4000

fprintf('%8.4g\n',123.4) ----|----|

123.4

Example: floating point (real) data is displayed using %e, %f and %g

format specifiers

Page 8: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

fprintf(‘%c\n’,‘s’) ----|----|

s

fprintf(‘%s\n’, ‘string’) ----|----|

string

fprintf(‘%8s\n’, ‘string’) ----|----|

string

fprintf(‘%-8s\n’, ‘string’) ----|----|

String (left justified)

Example: character data may be displayed with %c or %s format specifiers.

Page 9: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

x = 0:.1:1;

y = [x; exp(x)];

%fid = fopen('exp.txt', 'w+');

fprintf(fid, '%6.2f %12.8f\n', y);

%fclose(fid)

fprintf( ...

'A unit circle has circumference %g radians.\n', 2*pi)

A unit circle has circumference 6.283186 radians.

B = [8.8 7.7; 8800 7700]

fprintf(1, 'X is %6.2f meters or %8.3f mm\n', 9.9, 9900,

B)

X is 9.90 meters or 9900.000 mm

X is 8.80 meters or 8800.000 mm

X is 7.70 meters or 7700.000 mm

Examples:

Page 10: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Two common ways

Direct input through the keyboard (using the input function)

•Good for individual input.

•Not so good for large amount of data.

Use existing data stored in a file and import them into Matlab.

•Good for any data set size.

•Need to know the format of the data.

Page 11: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Different types of data files

•Text based files = formatted data for user usage. (.txt .dat )

Usually follows the American Standard Code for Information Interchange (ASCII)

Depending on the type of data file Different import function

Binary files = Pre-converted data for computer usage. (.bin .mat)

•Software specific format = formatted data for software usage (.xls)

Page 12: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Function load

Syntax: load filename

load filename loads all the variables from filename

If filename has an extension other than .mat, load

treats the file as ASCII data.

If filename has no extension, load looks for file

named filename or filename.mat and treats it as a

binary MAT-file.

0 0

1.0000 0.3090

2.0000 0.5878

3.0000 0.8090

4.0000 0.9511

5.0000 1.0000

6.0000 0.9511

7.0000 0.8090

8.0000 0.5878

9.0000 0.3090

10.0000 0.0000

test1.txt

>> load test1.txt;

>> whos

Name Size Bytes Class

test1 11x2 176 double array

Page 13: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Function dlmread

Syntax: A = dlmread('filename', 'delimiter');

dlmread command works even if the contents of

filename has spaces:

>> A=dlmread('test2.txt',';')

A =

7.2000 8.5000 6.2000 6.6000

5.4000 9.2000 8.1000 7.2000

7.2;8.5;6.2;6.6

5.4;9.2;8.1;7.2

test2.txt

7.2; 8.5; 6.2; 6.6

5.4; 9.2; 8.1; 7.2

test3.txt

Page 14: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Function textread

Syntax: [A,B,C,...] = textread('filename','format')

[A,B,C,...] = textread('filename','format',N)

[A,B,C,...] = textread('filename','format') reads data from the file filename into

the variables A,B,C, and so on, using the specified format, until the entire file is

read.

textread is useful for reading text files with known mixed formats. Both fixed

and free format files can be handled.

The format needs to be specified with a specifier like fprintf

%s for string, %f for fix point notation %d for integers

[A,B,C,...] = textread('filename','format',N) reads data from the file 'filename' N

times.

Page 15: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Ann Type1 12.34 45 Yes

Joe Type2 45.67 67 No

test4.txt

>> [A B C D E]=textread('test4.txt',' %s %s %f %f %s')

>> [A B C D E]=textread('test4.txt',' %s %s %f %f %s',1) A =

'Ann'

'Joe'

B =

'Type1'

'Type2'

C =

12.3400

45.6700

D =

45

67

E =

'Yes'

'No'

A =

'Ann'

B =

'Type1'

C =

12.3400

D =

45

E =

'Yes'

Page 16: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Function fscanf : Read formatted data from file

Syntax: A = fscanf(fid, format)

[A,count] = fscanf(fid, format, size)

A = fscanf(fid, format) reads data from the file specified by fid, converts it

according to the specified format string, and returns it in matrix A. Argument fid is an

integer file identifier obtained from fopen. format is a string specifying the format of the

data to be read. [A,count] = fscanf(fid, format, size) reads the amount of data specified by

size, converts it according to the specified format string, and returns it along with a count

of values successfully read. size is an argument that determines how much data is read.

Options

n Read at most n numbers, characters, or strings.

inf Read to the end of the file.

[m,n] Read at most (m*n) numbers, characters, or strings. Fill a matrix of at most m

rows in column order. n can be inf, but m cannot.

Page 17: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data

Example 1:

fid = fopen('exp.txt', 'r');

a = fscanf(fid, '%g %g', [2 inf]) % It has two rows now.

a = a';

fclose(fid)

0 1

0.1 1.10517092

0.2 1.22140276

0.3 1.34985881

0.4 1.4918247

0.5 1.64872127

0.6 1.8221188

0.7 2.01375271

0.8 2.22554093

0.9 2.45960311

1 2.71828183

1 12 3 4 8 xdata.txt

Example 2:

clc;clear;

fileID=fopen(‘xdata.txt', 'r' );

[a,nvals]=fscanf( fileID,'%d ',inf);

fclose( fileID);

a

nvals

a =

1

12

3

4

8

nvals =

5

Page 18: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Data Format Sample

File Extension

Matlab function

1 2 3 4 5

6 7 8 9 10

.txt .dat

or other

load

1; 2; 3; 4; 5

6; 7; 8; 9; 10

or

1, 2, 3, 4, 5

6, 7, 8, 9, 10

.txt .dat .csv

or other

dlmread

or

csvread

Ann Type1 12.34 45 Yes

Joe Type2 45.67 67 No

.txt .dat

or other

textread

or

fscanf

Grade1 Grade2 Grade3

91.5 89.2 77.3

88.0 67.8 91.0

67.3 78.1 92.5

.txt .dat

or other

textread

or

fscanf

Importing data

Page 19: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Tips for loading data

Use for loops to load entire series of files

for j=1:N

end

0 0

1.0000 0.3090

2.0000 0.5878

3.0000 0.8090

4.0000 0.9511

5.0000 1.0000

6.0000 0.9511

7.0000 0.8090

8.0000 0.5878

9.0000 0.3090

10.0000 0.0000

test001.txt

0 0

1.0000 0.3090

2.0000 0.5878

3.0000 0.8090

4.0000 0.9511

5.0000 1.0000

6.0000 0.9511

7.0000 0.8090

8.0000 0.5878

9.0000 0.3090

10.0000 0.0000

test002.txt

0 0

1.0000 0.3090

2.0000 0.5878

3.0000 0.8090

4.0000 0.9511

5.0000 1.0000

6.0000 0.9511

7.0000 0.8090

8.0000 0.5878

9.0000 0.3090

10.0000 0.0000

test003.txt

name1=‘test00';

name2=num2str(j);

name3=‘.txt’;

NAME=[name1 name2 name3];

F=load(NAME);

A(:,1)= F(:,1);

A(:,j+1)= F(:,2);

Page 20: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Three Steps:

Functions fopen, fprintf and fclose

Exporting data

1. fopen: opens a file or obtain information about open files

Syntax: fid = fopen(filename,permission)

permission= 'r‘ Open file for reading (default).

'w‘ Open file, or create new file, for writing; discard existing contents, if any.

'a‘ Open file, or create new file, for writing; append data to the end of the file.

Page 21: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data

fopen file permissions

‘r’ Open an existing file for reading only

‘r+’ Open an existing file for reading and writing

‘w’ Delete the contents of an existing file (or create a new file) and open it for

writing only.

‘w+’

Delete the contents of an existing file (or create a new file) and open it for

reading and writing

‘a’ Open an existing file (or create a new file) and open it for writing only,

appending to the end of file.

‘a+’

Open an existing file (or create a new file) and open it for reading and

writing, appending to the end of file.

Page 22: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Functions fopen,fprintf and fclose

Exporting data

fprintf: Used the same way as display function, except that this time, the

formatting of the data is retained in the data file.

Syntax: fprintf(fid,format,A,...)

Formats the data in the real part of matrix A (and in any additional matrix

arguments) under control of the specified format string, and writes it to the

file associated with file identifier fid.

fclose: close one or more open files

Syntax: fclose(fid)

2

3

Page 23: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data

x = 0:0.1:1;

Y = [x; exp(x)];

0.00 1.00000000

0.10 1.10517092

0.20 1.22140276

0.30 1.34985881

0.40 1.49182470

0.50 1.64872127

0.60 1.82211880

0.70 2.01375271

0.80 2.22554093

0.90 2.45960311

1.00 2.71828183

Exp.txt

Functions fopen, fprintf and fclose

File created in

current directory fid = fopen('Exp.txt','w');

fprintf(fid,'%6.2f %12.8f \n',Y);

fclose(fid)

Page 24: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Function xlsread reads MS Excel files

Syntax: num = xlsread(filename)

num = xlsread(filename, -1)

num = xlsread(filename, sheet, 'range‘ )

num = xlsread(filename) returns numeric data in double array num from the

first sheet in the Microsoft Excel spreadsheet file named filename. The filename

argument is a string enclosed in single quotes.

num = xlsread(filename, -1) opens the file filename in an Excel window,

enabling you to interactively select the worksheet to be read and the range of

data on that worksheet to import.

num = xlsread(filename, sheet, 'range') reads data from a specific rectangular

region (range) of the worksheet specified by sheet.

Page 25: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: xlsread

A = xlsread('testdata1.xls')

A =

1 6

2 7

3 8

4 9

5 10

1 6

2 7

3 8

4 9

5 10

testdata1.xls

Page 26: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: xlsread

A = xlsread('testdata1.xls‘,-1)

A =

1 6

2 7

3 8

4 9

5 10

1 6

2 7

3 8

4 9

5 10

testdata1.xls

Page 27: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: xlsread

A = xlsread('testdata1.xls‘,1,’A4:B5’)

A =

4 9

5 10

1 6

2 7

3 8

4 9

5 10

testdata1.xls

Page 28: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data (more…)

Function xlswrite

Syntax: xlswrite(filename, M)

xlswrite(filename, M, sheet, 'range')

xlswrite(filename, M) writes matrix M to the Excel file filename. The filename input is a

string enclosed in single quotes. The input matrix M is an m-by-n numeric, character, or

cell array, where m < 65536 and n < 256. The matrix data is written to the first worksheet

in the file, starting at cell A1.

xlswrite(filename, M, sheet, 'range') writes matrix M to a rectangular region specified by

range in worksheet sheet of the file filename.

Page 29: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data (more…)

Example: xlswrite

xlswrite('testdata', [12.7 5.02 -98 63.9 0 -.2 56])

d = {'Time', 'Temp'; 12 98; 13 99; 14 97};

s = xlswrite('tempdata.xls', d, 'Temperatures', 'E1')

Time Temp

12 98

13 99

14 97

tempdata.xls

Page 30: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Function csvread

Syntax: M = csvread(filename)

M = csvread(filename, row, col)

M = csvread(filename, row, col, range)

M = csvread(filename) reads a comma-separated value formatted file, filename. The

filename input is a string enclosed in single quotes. The result is returned in M. The file

can only contain numeric values.

M = csvread(filename, row, col) reads data from the comma-separated value formatted

file starting at the specified row and column. The row and column arguments are zero

based, so that row=0 and col=0 specify the first value in the file.

M = csvread(filename, row, col, range) reads only the range specified. Specify range

using the notation [R1 C1 R2 C2] where (R1,C1) is the upper left corner of the data to be

read and (R2,C2) is the lower right corner. You can also specify the range using

spreadsheet notation, as in range = 'A1..B7'.

Page 31: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: csvread

02, 04, 06, 08, 10, 12

03, 06, 09, 12, 15, 18

05, 10, 15, 20, 25, 30

07, 14, 21, 28, 35, 42

11, 22, 33, 44, 55, 66

csvlist.dat

csvread('csvlist.dat')

ans =

2 4 6 8 10 12

3 6 9 12 15 18

5 10 15 20 25 30

7 14 21 28 35 42

11 22 33 44 55 66

Page 32: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: csvread

m = csvread('csvlist.dat', 2, 0)

m =

5 10 15 20 25 30

7 14 21 28 35 42

11 22 33 44 55 66

To read the matrix starting with zero-based row 2,

column 0, and assign it to the variable m,

Page 33: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Importing data (more…)

Example: csvread

m = csvread('csvlist.dat', 2, 0, [2,0,3,3])

m =

5 10 15 20

7 14 21 28

To read the matrix bounded by zero-based (2,0) and

(3,3) and assign it to m,

Page 34: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data (more…)

Function csvwrite

Syntax: csvwrite(filename,M)

csvwrite(filename,M,row,col)

csvwrite(filename,M) writes matrix M into filename as comma-separated values. The

filename input is a string enclosed in single quotes.

csvwrite(filename,M,row,col) writes matrix M into filename starting at the specified row

and column offset. The row and column arguments are zero based, so that row=0 and

C=0 specify the first value in the file.

Page 35: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data (more…)

Example: csvwrite

m = [3 6 9 12 15; 5 10 15 20 25; ...

7 14 21 28 35; 11 22 33 44 55];

csvwrite('csvlist.dat',m)

type csvlist.dat

3,6,9,12,15

5,10,15,20,25

7,14,21,28,35

11,22,33,44,55

Page 36: Formatted Input/Output Operations - Marmara Üniversitesimimoza.marmara.edu.tr/~byilmaz/CSE123_Lecture9_2012.pdf · 2012-12-14 · Formatted Input/Output Operations . Data Import-Export

Exporting data (more…)

Example: csvwrite

m = [3 6 9 12 15; 5 10 15 20 25; ...

7 14 21 28 35; 11 22 33 44 55];

csvwrite('csvlist.dat',m,0,2)

type csvlist.dat

,,3,6,9,12,15

,,5,10,15,20,25

,,7,14,21,28,35

,,11,22,33,44,55