File Input and Output File Input and Output 1 / 9 File - - PowerPoint PPT Presentation

file input and output
SMART_READER_LITE
LIVE PREVIEW

File Input and Output File Input and Output 1 / 9 File - - PowerPoint PPT Presentation

File Input and Output File Input and Output 1 / 9 File input/output input function reads values entered by a user. File Input and Output 2 / 9 File input/output input function reads values entered by a user. load function read data from a


slide-1
SLIDE 1

File Input and Output

File Input and Output 1 / 9

slide-2
SLIDE 2

File input/output

input function reads values entered by a user.

File Input and Output 2 / 9

slide-3
SLIDE 3

File input/output

input function reads values entered by a user. load function read data from a file.

File Input and Output 2 / 9

slide-4
SLIDE 4

File input/output

input function reads values entered by a user. load function read data from a file. save function writes from a matrix to a data file.

File Input and Output 2 / 9

slide-5
SLIDE 5

File input/output

input function reads values entered by a user. load function read data from a file. save function writes from a matrix to a data file. However, load will only work as long as the data has the same values

  • n each line and these values are of the same data type, so that they

can be stored in a matrix.

File Input and Output 2 / 9

slide-6
SLIDE 6

File input/output

input function reads values entered by a user. load function read data from a file. save function writes from a matrix to a data file. However, load will only work as long as the data has the same values

  • n each line and these values are of the same data type, so that they

can be stored in a matrix. If the file has different formats we need lower level file Input/Output functions.

File Input and Output 2 / 9

slide-7
SLIDE 7

Opening and closing files

fopen opens a file for reading, writing or appending.

File Input and Output 3 / 9

slide-8
SLIDE 8

Opening and closing files

fopen opens a file for reading, writing or appending. fopen returns −1 if not successful in opening the file, or an integer value that becomes the file identifier. fid = fopen('filename','permission string')

File Input and Output 3 / 9

slide-9
SLIDE 9

Opening and closing files

fopen opens a file for reading, writing or appending. fopen returns −1 if not successful in opening the file, or an integer value that becomes the file identifier. fid = fopen('filename','permission string') permission strings include:

1

r read (default)

2

w write

3

a append

>>help fopen for other permissions

File Input and Output 3 / 9

slide-10
SLIDE 10

Opening and closing files

After calling fopen, value returned must be checked to make sure that the file has been successfully opened.

File Input and Output 4 / 9

slide-11
SLIDE 11

Opening and closing files

After calling fopen, value returned must be checked to make sure that the file has been successfully opened. Use fclose to close the file after finishing reading, writing or appending them.

File Input and Output 4 / 9

slide-12
SLIDE 12

Opening and closing files

After calling fopen, value returned must be checked to make sure that the file has been successfully opened. Use fclose to close the file after finishing reading, writing or appending them. fclose returns 0 if the file close was successful, or -1 if not. One must also check that the file has been closed properly at the end

  • f the program.

File Input and Output 4 / 9

slide-13
SLIDE 13

fscanf

fscanf will read data from a file directly into a matrix

File Input and Output 5 / 9

slide-14
SLIDE 14

fscanf

fscanf will read data from a file directly into a matrix Each line of the data file is stored as a column of the matrix

File Input and Output 5 / 9

slide-15
SLIDE 15

fscanf

fscanf will read data from a file directly into a matrix Each line of the data file is stored as a column of the matrix The matrix may need to be reformatted to get it back into the

  • riginal form from the file

Usage: mat = fscanf(fid,'format',[dimensions])

File Input and Output 5 / 9

slide-16
SLIDE 16

fscanf

fscanf will read data from a file directly into a matrix Each line of the data file is stored as a column of the matrix The matrix may need to be reformatted to get it back into the

  • riginal form from the file

Usage: mat = fscanf(fid,'format',[dimensions])

1

format includes conversion characters we used for fprintf, e.g %d - integers %f - floats (decimals)

2

dimensions specify the desired dimensions of mat

3

inf can be used for the second dimension if this value is not known

File Input and Output 5 / 9

slide-17
SLIDE 17

fscanf

fscanf will read data from a file directly into a matrix Each line of the data file is stored as a column of the matrix The matrix may need to be reformatted to get it back into the

  • riginal form from the file

Usage: mat = fscanf(fid,'format',[dimensions])

1

format includes conversion characters we used for fprintf, e.g %d - integers %f - floats (decimals)

2

dimensions specify the desired dimensions of mat

3

inf can be used for the second dimension if this value is not known

File Input and Output 5 / 9

slide-18
SLIDE 18

Exercise

Download the provided emissions.txt file and save it in your current

  • directory. Write a script that performs the following tasks

1 Open the file 2 Check to make sure that the file has been open successfully or

  • therwise and print out the appropriate message

3 Close the file and check to make sure that the file has been closed

successfully.

File Input and Output 6 / 9

slide-19
SLIDE 19

Writing to files

fprintf - write one line at a time to a file.

File Input and Output 7 / 9

slide-20
SLIDE 20

Writing to files

fprintf - write one line at a time to a file. Usage: fprintf(fid,'format',variable(s))

File Input and Output 7 / 9

slide-21
SLIDE 21

Writing to files

fprintf - write one line at a time to a file. Usage: fprintf(fid,'format',variable(s)) fprintf returns the number of bytes that was written to the file

File Input and Output 7 / 9

slide-22
SLIDE 22

Exercise

Visualizing the trend of concentration of C02 in the atmosphere

  • ver time

The file emissions.txt (downloaded from NOAA) contains the average C02 concentration per year.

1 Use fscanf to read the data in the file emissions.txt 2 Use the data to plot the growth of concentration of C02 over time 3 Use the data to plot the annual percentage increase in C02

concentration over time.

4 Save the annual percentage increase values in a file. File Input and Output 8 / 9

slide-23
SLIDE 23

Exercise

1960 1970 1980 1990 2000 2010 Year 320 330 340 350 360 370 380 390 400 C02 concentation in ppm Global C0 2 concetration 1960 1970 1980 1990 2000 2010 2020 year 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 % increase in average Annual percentage increase

File Input and Output 9 / 9