EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Write to File
 

Matlab Write to File

Updated March 6, 2023

Matlab Write to File

 

 

Introduction to Matlab Write to File

In Matlab, fprintf function is used to write data to a text file. This function will first apply firstspec to all the elements of the input arrays in the column order and then will write the data into a text file. We need to use fopen function in conjunction with the fprintf function, so that we can open the file first and then write the required data to it.

Watch our Demo Courses and Videos

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

Syntax to write data to a file:

fprintf(formatSpec, X1, X2, ..., Xn)

fprintf(file_ID, formatSpec, X1, X2, ..., Xn)

Description:

  • fprintf(formatSpec, X1, X2, …, Xn)will write the data to our file by applying the formatSpec to all the elements of the input arrays X1, X2, …Xn in the column order
  • fprintf(file_ID, formatSpec, X1, X2, …, Xn) will write the data to our file ‘file_ID’ by applying the formatSpec to all the elements of the input arrays X1, X2, …Xn in the column order

Examples of Matlab Write to File

Let us now understand the code to use the above 2 functions to write to a file

Example #1

In this example, we will use the fprintf function to write data to our file. The data to be written will be output of a mod function. Below are the steps to be followed:

  1. Initialize the input array
  2. Pass this input arrayand mod function as an argument to the fprintf function
  3. Use ‘%d’ inside fomatspec to print each value of the array

Code:

a = [11 3 5 10 14 12 7 3 2];

[Initializing theinput array]

fprintf('%d\n',mod(a,5));

[Using the fprintf function to write the data into the file. ‘\n’ will ensure a line break after every output]

This is how our input and output will look like in MATLAB:

Input:

Matlab Write to File-1.1

Output:

 Matlab Write to File-1.2

As we can see in the output, the fprintf function has the written output of the mod function.

In the next example, we will create a test file and will write data to it.

Example #2

In this example, we will use the fprintf function to write data to a test file. The data to be written will be an array of integers. Below are the steps to be followed:

  • Initialize the input data to be written in the file
  • Use fopen function to open the file called testfile. Pass the second argument ‘w’ to the fopen function, which signifies that we will be writing to this file
  • Use the fprintf function to write the input data to this file
  • Use ‘%3d’ inside fomatspec to print each value of the array at the required distance
  • Close the file
  • Use the type function to confirm if the data is written in the file

Code:

X = [1 4 3 5; 4 6 1 5; 4 3 2 5; 6 7 5 4];

[Initializing the input data]

file_ID = fopen('testfile.txt','w');

[Initializing and opening the test file]

fprintf(file_ID,'%3d %3d %3d %3d\n',X);

[Using fprintf function to write the input to the test file]

fclose(file_ID);

[closing the file once the data is written]

type('testfile.txt')

[Using the type function to confirm that data is written to the file]

This is how our input and output will look like in MATLAB:

Input:

Output-1.3

Output:

 Output-1.4

As we can see in the output, the fprintf function has written our data to the file created. Please note that, we must pass ‘w’ as an argument to the fopen function, else we will only have the read-only permission to the file and would not be able to write to it or make any changes.

Let us understand this with an example.

Example #3

In this example, we will use the fprintf function to write data to a test file. We will try to edit the same file created in the above example. However, in this case, we will not pass ‘w’ as an argument to the fopen function. Below are the steps to be followed:

  • Initialize the input data to be written in the file
  • Use fopen function to open a file called testfile.
  • Use the fprintf function to write the input data to this file
  • Use ‘%3d’ inside fomatspec to print each value of the array at the required distance
  • Close the file
  • Use the type function to confirm if the data is written in the file

Code:

X = [11 412 5; 14 0 1 15; 1 3 1 5; 3 7 15 4];

[Initializing the input data]

file_ID = fopen('testfile.txt');

[Initialize and open the test file. Please note that we have not passed the argument ‘w’ in this case]

fprintf(file_ID,'%3d %3d %3d %3d\n',X);

[Using fprintf function to write the input to the test file]

fclose(file_ID);

[closing the file once the data is written]

type('testfile.txt')

[Using the type function to confirm that data is written to the file]

This is how our input and output will look like in MATLAB:

Input:

Output-1.5

Output:

 Output-1.6

As we can see in the output, the fprintf function is not able to write the new data to our file. This is because we only had read-only permission for this file and were not able to edit it.

Conclusion

  • We use fprintf function in MATLAB to write our data to a text file
  • This function is used in conjunction with the fopen function
  • An argument ‘w’ must be passed to the fopen function so that we have the edit permission

Recommended Articles

This is a guide to Matlab Write to File. Here we also discuss the introduction and syntax of matlab write to file along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. AWS Direct Connect vs VPN
  2. VPN Applications for Android
  3. Why we use VPN?
  4. OpenSSL Alternatives
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
EDUCBA

*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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW