EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Fread
 

Matlab Fread

Updated March 14, 2023

Matlab Fread

 

 

Introduction to Matlab Fread

The fread statement is used to read a binary file, the binary file means with an extension .bin. For reading a binary file using a fread statement first we need to open that binary file using a fopen statement and after reading the file we just need to close that file using a fclose statement. If we need to read an array from that binary file we need to mention the size and also precision.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

The syntax for fread statements are as follows:

r1= fread(fileID1)
r1 = fread(fileID1,sizer)
r1 = fread(fileID1,sizer,precision)
r1 = fread(fileID1,sizer,precision,skip)
r1 = fread(fileID1,sizer,precision,skip,machinefmt)
[r1,count] = fread(___)

How does Matlab Fread work?

Steps to read the data from binary file:

1. open the binary file using fopen Matlab function

2. By using file identifier file is accessible

3. use the appropriate syntax (fread ) to read the file

Examples to Implement Matlab Fread

Below are the examples of Matlab Fread:

Example #1

Let us consider one example, in this example first, we open a binary file for write operation for these we use a fopen statement. Fopen statement is used to open a file or obtain information about an open file. In fopen statement, we take a file name in a single inverted comma and specify the type of access mode that iswrite (‘w’) in another single inverted comma, and these two arguments are separated by a comma and assign this fopen statement for file identifier of the open file (fileID1). Then we take a fwrite statement two write ten numbers in that binary file. Then close that open file using a fclose statement. And simply we again open a binary file using a fopen statement without specifying the type of access mode because a default mode is read mode. Then we use a fread statement, basically, a fread statement is used for reading a binary file, we read that binary file and store it in variable A1, and display it.

Code:

clc ;
clear all;
close all;
fileID1 = fopen('ten.bin','w');
fwrite(fileID1,[1:10]);
fclose(fileID1);
fileID1 = fopen('ten.bin');
A1 = fread(fileID1)

Output:

matlab fread1

Explanation: In figure 1 we can see that the data in a binary file ( ten.bin ) is read by using fread Matlab function. After reading the data is assigned to a variable A1. And after running the code we get the assigned data to the command window.

Example #2

Let see one another example related to the fread statement, in this example, we take one binary file with extension .bin. Firstly we create a matrix and it writes on that binary file. The binary file name is mydoc1.bin, we open that file using fopen statement, and a specified access mode to write it will assign to the file. Then we write on that binary file by using a fwrite statement then we close that file using the file identifier of the open file and fclose statement. Then we again need to open that file using a fopen statement. Then we use a fread statement, fread is used for reading a binary file. We take a fread in parenthesis file identifier, matrix size, and specify the size of that data source is double. Then it displays the result that what is written on that binary file. And we close that open binary file using a fclose statement.

Code:

clc;
clear all;
close all;
fileID = fopen('mydoc1.bin','w');
fwrite(fileID,magic(3),'double');
fclose(fileID);
fileID = fopen('mydoc1.bin');
R = fread(fileID,[3 3],'double')
fclose(fileID);

Output:

fclose statement

Explanation: In figure 2 we can see that the data in a binary file ( mydoc.bin ) is read by using fread Matlab function.

Example #3

Let us consider another example, in this example, we take one binary file with extension .bin. Firstly we create a matrix and it writes on that binary file. The binary file name is mydoc1.bin, we open that file using fopen statement, and a specified access mode to write it will assign to fileID. Then we write on that binary file by using a fwrite statement then we close that file using the file identifier of the open file and fclose statement. Then we again need to open that file using a fopen statement. Then we use a fread statement, fread is used for reading a binary file. We take a fread in parenthesis only a binary file identifier on an open binary file and then return data stored in a variable R. Then we use a whos function, whose function is a list a variable on that workshop with its size and type. Then we simply close that opened binary file using a fclose statement.

Code:

clc;
clear all;
close all;
fileID = fopen('mydoc.bin','w');
fwrite(fileID,[1:4]);
fclose(fileID);
fileID = fopen('mydoc.bin');
R = fread(fileID);
whos R
fclose(fileID);

Output:

binary file with extension

Conclusion

In this article we saw the basic concept of fread statement, basically fread is nothing but reading a file or obtaining information into the files. We also saw the different syntax for the fread statement used in Matlab code. Also, we saw the different examples related to fread.

Recommended Articles

This is a guide to Matlab Fread. Here we discuss an introduction to Matlab Fread, syntax, how does it work, examples with code, and output. You can also go through our other related articles to learn more –

  1. Matlab Plot Colors
  2. Heaviside MATLAB
  3. MATLAB Derivative
  4. Factorial in Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW