Illustration of Advanced file input and output:
For illustration, to refer to the third number in the first element of the cell array:
The script which reads in this data and echo-prints it is shown here:
Executing this script generates the following results which are as shown below:
File close successful
To conclude, we know four techniques of reading from a file. The function load will only work when the values in the file are all of similar type and there is similar number on every line in the file, so that they can be read into the matrix. If this is not the situation, lower level functions should be used. To use these, the file should be opened first and then closed whenever the reading has been done. The fscanf function will read into the matrix, converting the characters to their ASCII equivalents. The textscan function will rather read into a cell array which stores each column from the file into individual column vectors of the cell array. At last, the fgetl function can be used in a loop to read each line from the file as an individual string; the string manipulating functions should then be used to break the string into pieces and convert to numbers.