EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests
 11+ Hours of HD Videos
5 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more