File I/O Flashcards
Reading values/texts from excel file
Reading everything in excel file
[num,text,raw] = xlsread(‘filename’)
[~ ~ everything] = xlsread(‘filename’)
Opening a txt file
Unique file identifier
Types of permissions & purposes
Fid=fopen(filename, permission)
Fid = unique identifier
Permissions:
rt : open & read
wt: open & write, discard original content
at: open/create & write, continue from end of file
r+t : open for read & write
w+t : open/create for read & write, discard original
a+t : open/create for read & write, continue from end of file
Closing a txt file
Fclose(fid)
Checking if txt file is opened successfully
If fid 0 then file successfully opened
Reading txt file as set of strings one line at a time
Fgets(fid) gets strings from txt one line at a time
Checking if end of txt file is reached
If ischar(fget(fid)) != 1 then end of txt file
Ischar returns 1 whenever there are still strings to read from
Opening a binary file
Permissions
Fid=fopen(filename,’permission’)
Permissions:
r : open & read
w : open & write, discard old data
a : open/create & write, add to end of file
r+ : open for read & write
w+ : open/create for read & write, discard old data
a+ : open/create for read & write, add to end of file
Writing to a binary file
Fwrite(fid,data,’data type’)
Correct data type must be used
Eg:
If data = positive integer values, data type must be double
Reading from binary file
Data = fread(fid,’data type’)
Correct data type must be used to read data. If data is double, cannot use char to read