Section 5.2 The fscanf()
Command
There are multiple ways to customize a script file so as to respond to a user’s needs. Since the script file shares the variable workspace with the command window, one way is to simply create a variable in the command window and assign it a certain value before running the script file. That value can then be used within the script file. But that’s not a very clean way to go about this. A better method is to have the script ask for user input or read input from a file, for example via the fscanf()
command.
The fscanf()
command differs a bit from its C counterpart: The syntax is slightly different and in addition, in MATLAB it is vectorized.
Just as in C, before reading from a file it needs to be opened:
>> fid = fopen('filename');
Once a file is open you can refer to it using the chosen variable (fid in the above example).
The default permission for opening a file is for reading ('r'
) and you do not need to spell this out although you could:
>> fid = fopen('filename', 'r');
Once a file is open, you can read data from it using the fscanf()
command. Again, there are multiple options. The most simple one is:
>> A = fscanf(fid, format);
This command reads all of the data from the open text file (referred to by fid
) into a column vector A
, with values from the file interpreted according to the given format specifier. If fscanf cannot match the format specifier to the data in the file (for example trying to interpret a character as a float), it reads only the portion that matches and then stops processing.
Let’s take a look at an example. Suppose you have a file named 'numbers.txt'
in your working directory with the following contents:
Then
>> fid = fopen('numbers.txt');
>> A = fscanf(fid, '%f')
A =
23.5000
34.3000
33.1000
When you are done reading from a file you should close it using
If you do not wish to read all of the data at once you can specify how much data to read and in what shape to store it:
>> A = fscanf(fid, format, size);
This reads the file data into an array, A, whose dimensions are specified in size
, and positions the file pointer after the last value read. The fscanf()
function populates A column by column. In addition, size
must be a positive integer or have the form [m n]
, where m and n are positive integers that describe the shape of the matrix A.
Continuing on with the file 'numbers.txt'
from above, here are a few more examples:
>> fid = fopen('numbers.txt');
>> A = fscanf(fid, '%f', 2)
>> fclose(fid);
Here, we specified to only read two values from the file.
>> fid = fopen('numbers.txt');
>> A = fscanf(fid, '%f', [1 3])
>> fclose(fid);
A =
23.5000 34.3000 33.1000
We specified that we wanted to store the data in the form of a 1x3 matrix, which is row vector.
To sum things up:
The default permission for opening a file is 'r'
.
format
is the format specifier string, similar to C: '%lu %d %d %d'
size
is an optional array indicating how many items to read from the file. The default is to read the whole file into a column vector. The resulting values are assigned to the variable A.
Activity 5.1.
Let’s say we wanted to read the following file, which I named 'numbers.txt'
on my computer (it contains a few more numbers than the previous one):
23.5 34.3 33.1
63.9 234.0 34.2
54.3 34.2 67.9
234.3 22.5 34.7
78.5 57.4 44.0
22.4 54.3 64.7
34.5 65.3 77.2
23.5 54.7 32.4
43.5 34.2 44.4
43.0 22.1 45.2
344.5 42.3 76.8
234.1 11.2 22.3
45.2 34.5 32.1
42.6 33.2 12.3
Below are some examples that use the fscanf function to read (parts of) the file. Copy this code into a script file and run it line by line (use breakpoints and the step-by-step execution feature). Before each command, try to predict what it will do.
fname = 'numbers.txt';
fptr = fopen(fname); % could use 'r', but default is for reading anyway
A = fscanf(fptr, '%f', 6); % read 6 elements into a vector A
B = fscanf(fptr, '%f', [3, 4]); % read from where you are into a matrix
% B with three rows and 4 columns (fills
% up B column by column)
C = fscanf(fptr, '%f'); % read from where you are to the end of
% the file, store in vector C
%C = fscanf(fptr, '%f', [3, inf]); % read from where you are to the end
% of the file, store in a matrix of three
% rows and as many columns as necessary
fclose(fptr);
Now that you have studied the above examples, how could you use fscanf()
to read all of the numbers in the file and store them in a matrix A so that the elements in A are arranged exactly as they are in the file 'numbers.txt'
?