Mastering Low-Level File I/O in MATLAB
180 likes | 291 Vues
Explore advanced techniques for file input and output operations in MATLAB. Learn how to manipulate files at a lower level, handle mixed content, and improve data formatting. Open, read, write, and close files effectively using fopen, fprintf, fscanf, and more. Enhance your file handling capabilities in MATLAB.
Mastering Low-Level File I/O in MATLAB
E N D
Presentation Transcript
Advanced File I/O Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH) http://computer-history.info/Page4.dir/pages/IBM.7030.Stretch.dir/
Io? I/O? Io One of the moon’s of Jupiter (A Galilean satellite) File I/O Shorthand for file input and output. Refers to the process of reading data from files and writing data to files We have already covered basic file I/O! We need to cover more advanced low-level file I/O
File I/O Hard Disk Memory (RAM) File Input File Output Where files are stored Permanent: Stays even after computer is shut off Slow to read/manipulate Where variables are stored Temporary: Goes away when computer is shut off Fast to read/manipulate
File Output: A Review File Output: ‘save’:saves a matrix as an ASCII or .mat file • All values printed with the same formatting and delimiter • File will have consistent number of columns
File Output: A Review File Output: ‘dlmwrite’: saves a matrix as an ASCII file • Can specify the delimiter (must be a single character) • File will have consistent number of columns (can be formatted)
File Input: A Review File Input: ‘load’: loads in an ASCII file or .mat file • File must have consistent number of columns • All data must be numeric; no formatting options (loaded as class “double”)
What’s Next: Lower-Level File I/O What if you want to write/read a file that has… • Different formatting in each column • Different numbers of columns in some rows • Has both numbers and words/characters in a single row • Writing to a file • Reading from a file Variable in RAM File on Disk File Output File on Disk Variable in RAM File Input
Lower-Level File I/O • The ‘save’, ‘dlmwrite’, and ‘load’ commands do a lot of things automatically that we never are aware of • Lower-level file I/O requires a more step by step process • General steps for lower-level file I/O • Open the file • Read, Write, or Append to the file • Close the file Think about opening files in a filing cabinet
Opening and Closing Files fopen: opens a file ‘r’ – open for reading (default) ‘w’ – open for writing • Will erase/overwrite the file if it already exists ‘a’ – open and append to file • Will add to the end of an existing file • fopen returns a file identifier • Class: double (the value is an integer) • Used to identify the file later on in your code • -1 signifies an error in opening the file
Opening and Closing Files • Warning! Opening with write permission (‘w’) will erase the file if it exists!
Opening and Closing Files • After you are finished with a file, you should close it • Upon closing MATLAB, all open files are closed • Closing files in your code, prevents errors and unwanted behavior fclose • Returns 0 if close was successful • Returns –1 if there was an error in closing • Can test in an if statement
Opening and Closing Files • Typically, you will open and close files in a script or function • Should test to make sure there are no file opening errors
Writing to Files: fprintf • fprintf can also be used to write to files! • Typically used inside a loop
Reading Files with Mixed Content • ‘load’ can’t read files with numbers and characters/strings For all of these examples, the file must be first opened with ‘fopen’ and closed with ‘fclose’ after you are finished • ‘fscanf’ can read files with mixed content • Data is stored in a matrix • Automatically converts characters to ASCII values • Format of file is given in fprintf style • Automatically converts characters to ASCII values • ‘textscan’ can read files with mixed content • Data is stored in a cell array • Each cell can have different types of variables (strings, doubles, cells, etc…) • ‘fgetl’ reads a file line by line • Each line is stored as a string • Use string processing with strtokto split up each line into variables
Reading Files with Mixed Content • ‘textscan’ can read files with mixed content • Data is stored in a cell array • Each cell can contain any type of variable (char, double, cell) • File must already be opened with ‘fopen’ • Must close file with ‘fclose’ when finished
Reading Files with Mixed Content • ‘textscan’ stores data in a cell array Let’s experiment with this script and the resulting cell array FUN!!
Reading Inconsistently Formatted Files textscan reads all lines • Includes the header line • Will do this even if the header starts with ‘%’ • For more details, refer to in-class demo…
Final Thoughts MATLAB can read in ASCII files in many formats • If ‘load’ works, use it! • If file has a mix of numbers and letters use ‘textscan’ • textscan is quite flexible. Use fprintf style control characters • some files are too inconsistent to be read accurately by textscan • Alternative? Read in a file line by line using fgetl and strtok • I won’t test you on fgetl and strtok MATLAB can write ASCII files in many formats • Use fprintf inside a loop and print files line by line