A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Saturday, June 4, 2016

Working with Files in MATLAB- Part 2

In the last post we discussed how open and close the external files by MATLAB. In this post we will discuss some low level read and write functions to work with external files. Read Part 1 Here.
Low level I/O functions are used to read or write data to a file. It gives more control over the I/O operation if you have the full details of the file. The file must be opened before using these commands.

fscanf( ) - Read formatted data from a text file


fscanf is the basic low level I/O function like fprintf. It is used to read an entire file with some specified formatting.
[A, count] = fscanf(FID, ‘format’, size) reads the data throughout the entire file, convert it to a format defined by ‘format’ and save into the array A in column order. FID is the file identifier obtained from fopen( ) command. ‘format’ is a string containing the conversion specifications (which include a % character, an optional * to skip over the matched value, an optional width field and a conversion character as we discussed in fprintf).
The optional part, count, is an output argument that gives the number of elements successfully read. The size defines the dimensions of the output variable. If the function can’t match the ‘format’ to the data, it reads only the portion that matches and then stops processing.
The example below is used to read the numeric data in a text file.


fscanf can be used to read the text also as shown in the example below.


 

fread( ) -  Read binary data from file


fread function is mainly used for reading binary data.
X = fread(FID, size, ‘precision’) reads specified number of binary data from the file and writes it into matrix A.  MATLAB reads the entire file byte by byte and positions the file pointer at a point specified by the size or at the end of the file.
The argument size is optional. The optional argument ‘precision’ is to specify the type of the data to be read. It is a string contains data type specifier like 'int' or 'float', followed by an integer giving the size in bits. If not specified, the default data type is 'uint8'. If the precision is 'char' or 'char*1', MATLAB reads characters.
If an error occurs before reaching the end of file, only full elements read up to that point are used.
By default, numeric and character values are saved as 'double' arrays. To save the data in a data type other than double, modify the precision argument by first specifying the source format, then following it by '=>', and finally specifying the destination format.
For example,
        ‘double=>uint8’              Read in doubles, convert and save as an unsigned 8-bit integer
Examples:
Consider the texttest.txt file used in the pre3vious example.
>> fileID = fopen('texttest.txt','rt');
>> X= fread(fileID,7)

X =
    87
   101
   108
    99
   111
   109
   101
>> frewind(fileID)
>> X= fread(fileID,7,'uint8=>char')
X =
W
e
l
c
o
m
e
In the above example, when we used the default data type, the characters in the file are saved as numeric, the ASCII code of each character. When the precision is set to save as character the elements in the vector X is saved as characters. After the fread function is executed the file pointer will be at point after the letter ‘e’. frewind( ) is used to set the file pointer back to the start of file position. If we use the fread function without frewind, the file will be read from the current position of the file pointer. See the example below.
>> X= fread(fileID,7,'uint8=>char')'
X =
Welcome
>> Y = fread(fileID,4,'uint8=>char')'
Y =
 to
>> Z = fread(fileID,10,'uint8=>char')'
Z =
Electrical
submit to reddit