file input and output
play

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


  1. File Input and Output File Input and Output 1 / 9

  2. File input/output input function reads values entered by a user. File Input and Output 2 / 9

  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

  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

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

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

  7. Opening and closing files fopen opens a file for reading, writing or appending. File Input and Output 3 / 9

  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

  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: r read (default) 1 write w 2 append a 3 >>help fopen for other permissions File Input and Output 3 / 9

  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

  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

  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 of the program. File Input and Output 4 / 9

  13. fscanf fscanf will read data from a file directly into a matrix File Input and Output 5 / 9

  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

  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 original form from the file Usage : mat = fscanf(fid, ' format ' ,[dimensions]) File Input and Output 5 / 9

  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 original form from the file Usage : mat = fscanf(fid, ' format ' ,[dimensions]) format includes conversion characters we used for fprintf , e.g 1 %d - integers %f - floats (decimals) dimensions specify the desired dimensions of mat 2 inf can be used for the second dimension if this value is not known 3 File Input and Output 5 / 9

  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 original form from the file Usage : mat = fscanf(fid, ' format ' ,[dimensions]) format includes conversion characters we used for fprintf , e.g 1 %d - integers %f - floats (decimals) dimensions specify the desired dimensions of mat 2 inf can be used for the second dimension if this value is not known 3 File Input and Output 5 / 9

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

  19. Writing to files fprintf - write one line at a time to a file. File Input and Output 7 / 9

  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

  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

  22. Exercise Visualizing the trend of concentration of C 0 2 in the atmosphere over time The file emissions.txt (downloaded from NOAA) contains the average C 0 2 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 C 0 2 over time 3 Use the data to plot the annual percentage increase in C 0 2 concentration over time. 4 Save the annual percentage increase values in a file. File Input and Output 8 / 9

  23. Exercise Global C0 2 concetration Annual percentage increase 1 400 0.9 390 0.8 0.7 C0 2 concentation in ppm 380 % increase in average 0.6 370 0.5 360 0.4 350 0.3 340 0.2 330 0.1 320 0 1960 1970 1980 1990 2000 2010 1960 1970 1980 1990 2000 2010 2020 Year year File Input and Output 9 / 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend