Updated March 6, 2023
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.
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.
- If our location is the current folder: In this location, we just need to write the file name.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
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.
Now we can see data of A file as below mentioned screenshot as follows.
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.
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.
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.
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 –