Illustration of reading from files:
For illustration, suppose that there is a data file 'subjexp.dat' that has on each line a number followed by thecharacter code. The type function can be used to show the contents of this file (as the file does not have the default extension .m, the extension on the filename should be involved).
>> type subjexp.dat
The load function would not be capable to read this into the matrix as it holds both numbers and text. Rather than, the fgetl function can be used to read each line as a string and then the string functions are used to break up the numbers and characters. For illustration, the below just reads each line and prints the number with 2 decimal positions and then the rest of the string:
Here is an illustration of executing this script:
File close successful
In this illustration, in the loop each and every time the fgetl function reads one line into the string variable. The string function strtok is then used to store the number and character in individual variables, both of which are the string variables. The function str2num should then be used to convert the number stored in the string variable into a double variable so that computations could be performed on it.
Rather than of using the fgetl function to read one line, a file has been once opened the fscanf function could be used to read in from this file directly into a matrix.
Though, the matrix should be manipulated somewhat to get it back into the unique form from the file. The format of using the function is as shown below:
mat = fscanf(fid, 'format', [dimensions])