Follow this article to explore the implementation of the fread() function with different syntaxes and examples for reading binary files in MATLAB.
Why Is it Necessary to Read Binary Files
Binary files are important for a number of reasons, such as they are used to store a large variety of data types, including audio, images, videos, and more. They are effective in terms of reading as well as writing, especially for large files. The binary files are widely used in developing software applications or storing scientific data, such as experimental or simulation results.
How to Read a Binary Files in MATLAB
Reading binary files in MATLAB can easily be done through the built-in fread() function that efficiently allows reading data from a binary file. This function accepts a fileID that contains an open binary file, as an input parameter and provides us with a column vector that includes the entire binary data read from the file.
Syntax
We can implement MATLAB’s fread() function through the following syntaxes.
A = fread(fileID,sizeA)
A = fread(fileID,precision)
A = fread(fileID,sizeA,precision)
Here,
- The function A = fread(fileID) is responsible for reading binary data from a file to a column vector A positioning the file pointer at the end of the file marker. The file identifier fileID is used for identifying the binary file. The function fopen() is used for opening the binary file to obtain the value of fileID. After reading all binary data from the file, we need to close it using the fclose(fileID) function.
- The function A = fread(fileID,sizeA) is responsible for reading binary data from a file to an array A having size sizeA positioning the file pointer after the last read value. This function presents A in the column order.
- The function A = fread(fileID, precision) is responsible for interpreting binary data in the file in the form and size described by the variable precision.
- The function A = fread(fileID,sizeA, precision) is responsible for reading binary data from a file to an array A having size sizeA positioning the file pointer after the last read value. This function presents A in the column order. The binary data in the file is interpreted in the form as well as size described by the variable precision.
Example 1: How to Read Binary Data from a File in MATLAB?
This MATLAB code opens a binary file named TextFile1.bin using the fopen() function reads the binary data from the file and stores it in column vector A having class double using the fread() function.
A = fread(fileID)
fclose(fileID);
Example 2: How to Read Selected Rows and Columns from a File in MATLAB?
In this example, we open a binary file named cos_file.bin using the fopen() function, read the selected rows and columns from the file by passing the array size 3-by-6 as an argument, and store it in the matrix A using the fread() function.
A = fread(fileID,[3,6])
fclose(fileID);
Example 3: How to Read Entire File of int8 Precision in MATLAB?
In this MATLAB code, we use the fread() function to read binary data from the given file and store it in column vector A having the data type int8 in the form of precision.
A = fread(fileID,'int8')
fclose(fileID);
Example 4: How to Read Selected Rows and Columns of int32 Precision From a File in MATLAB?
The given example opens a binary file named cos_file.bin using the fopen() function, reads the selected rows and columns from the file by passing the array size 8-by-7 as an argument, and stores it in the matrix A using the fread() function. This function interprets the file data in the form of int32 precision.
A = fread(fileID,[8,7],'int32')
fclose(fileID);
Conclusion
Binary files are effective for storing a large variety of data types, such as audio, images, videos, and others. In MATLAB, the fread() is a built-in function responsible for reading binary data from a file to a column vector. This tutorial has presented an easy and simple implementation of the fread() function in MATLAB with examples that help us understand the function.