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 writematrix
 

Matlab writematrix

Updated March 6, 2023

Matlab writematrix

 

 

Introduction to Matlab writematrix

Matlab provides different types of functionality to the user; the writematrix is also one kind of functionality provided by Matlab. The writematrix provides the ability to write the homogeneous array into a comma delimited text file. In which that filename is a workspace variable name and it has a .txt extension. Sometimes writematrix is not able to write the file name at that time it constructs as matrix.txt. As per our requirement, we can use the Matlab writematrix function for different purposes. By using the Matlab writematrix function we can perform the different operations as per the requirement of the user.

Watch our Demo Courses and Videos

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

Syntax

There are multiple syntaxes available for the Matlab writematrix as follows.

writematrix(variable name)
writematrix(variable name, file name)

Explanation

In the first syntax, we use the writematrix function with a single parameter that is a variable name, here we write any name as a variable name. By using this syntax we can write the homogenous array into the comma delimited text file as per requirement and the extension of that file is .txt.

Similarly in the second syntax, we use the writematrix function with two different parameters such as variable name that we already discussed in the above syntax and file name, which means we can specify the file with .txt, .dat, .csv extension for delimited files. And some different file formats we can use in writematrix such as .xls, .xlsm, or .xlsx for Excel.

How writematrix works in Matlab?

Now let’s see how writematrix works in Matlab as follows.

Required input data:

Writematrix uses a specified matrix as input data.

Specified file name:

The filename should be a string or character vector and it depends on the location where we want to write the file. So it uses the different location format as follows.

  1. If our location is the current folder: In this location, we just need to write the file name.
  2. If our location is another folder: In this type, we need to write the path or we can also call it as the relative pathname of the folder where we want to store the file.
  3. If we need to store files on the remote location: In this type, we need to provide a URL or we need to provide the full path that contains the folder address.

If we provide the file name with an extension then writematrix functions determine the specified format of the file otherwise it uses comma-separated file format.

If the newly created file name exists in a folder then writematrix functions overwrite the file name.

Name-Value Pair for writematrix

This is an optional comma-separated pair of names and values, in which we can specify the multiple pair of name and value arguments as per the requirement. It contains the following options to write the file as follows.

  1. Type of file:

It is used to specify the comma-separated file that contains the file name and string. In which that file name is used as input argument and it included the different extension such as .txt, .dat, ot.csv for the delimited text files and .xls, .xlsm or .xlsx for excel file format

  1. WriteMode of file type:

In which we can specify the write mode of a comma-separated file that contains the pair of two values that is wrieMode and string and it depends on the file types as follows.

If we have a text file type then we use overwrite as the default write mode.

If we have a spreadsheet file type then we can use it in place as default write mode.

  1. The delimiter in writematrix:

By using the delimiter field we can write the different delimiters as per our requirement such as Comma, Space, Tab, Semicolon, etc.

  1. If we have spreadsheet file type:

In this file type, it contains a pair of sheets’ names and string arguments. Here we can assign the sheet name as per our requirement as well as we can also specify the index of the sheet. It uses different data types such as char, string, single, double and int8, etc.

Examples

Now let’s see the different examples of writematrix in Matlab for better understanding as follows.

Let’s see how we can write the matrix into the text file as follows.

First, we need to create the matrix. Here we use the magic function to create the n-by –n matrix as below.

A = magic(4)

Explanation

By using the above statement we created a 4 by 4 matrix. As shown below screenshot as follows.

Matlab writematrix output 1

After that, we need to write this matrix into the text file by using the following statement as follows.

writematrix(A)
type 'A.txt'

Explanation

In the above statement, we use the writematrix function, here A is the file name and we use the txt file type to create the text file of the matrix as shown. The final output of the above statement we illustrated by using the following screenshot as follows.

Matlab writematrix output 2

Now we can see data of A file as below mentioned screenshot as follows.

output 3

In the above example, we can use a different delimiter as per our requirements.

Now let’s see how we can write the matrix into the spreadsheet as follows.

First, we need to create the matrix. Here we use the magic function to create the n-by –n matrix as below.

A = magic(4)

Explanation

By using the above statement we created a 4 by 4 matrix. As shown below screenshot as follows.

output 4

After that, we need to write this matrix into the spreadsheet file by using the following statement as follows.

writematrix(A,'A.xls')

Explanation

By using the above statement we write the matrix into the spreadsheet. The final output of the above statement we illustrated by using the following screenshot as follows.

Matlab writematrix output 5

readmatrix('A.xls')

Explanation

By using the above statement we can read the content of the above-created spreadsheet.  The final output of the above statement we illustrated by using the following screenshot as follows.

output 6

In this way, we can perform the different operations by using the writematrix.

Conclusion

We hope from this article you learn Matlab writematrix. From the above article, we have learned the basic syntax of the writematrix and we also see different examples of the writematrix. From this article, we learned how and when we use Matlab writematrix.

Recommended Articles

This is a guide to Matlab writematrix. Here we discuss the basic syntax of the writematrix and also the different examples of the writematrix. You may also have a look at the following articles to learn more –

  1. Matlab boolean
  2. Matlab Mod
  3. Matlab Backslash
  4. Matlab Saveas

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
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?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW