Download presentation
Presentation is loading. Please wait.
Published byValentine Spencer Modified over 9 years ago
1
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/
2
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
3
Hard DiskMemory (RAM) Where variables are stored Temporary: Goes away when computer is shut off Fast to read/manipulate Where files are stored Permanent: Stays even after computer is shut off Slow to read/manipulate
7
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 1.Writing to a file 2.Reading from a file File on DiskVariable in RAM File on Disk
8
Think about opening files in a filing cabinet
9
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
10
Warning! Opening with write permission (‘w’) will erase the file if it exists!
11
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
12
Typically, you will open and close files in a script or function Should test to make sure there are no file opening errors
13
fprintf can also be used to write to files! Typically used inside a loop
14
‘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 strtok to split up each line into variables
15
‘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
16
‘textscan’ stores data in a cell array Let’s experiment with this script and the resulting cell array
17
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…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.