How to Export a Matrix as a CSV File in MATLAB?


In MATLAB, exporting a matrix as a CSV file is a common process which is used to save the numerical data stored in MATLAB matrix in such a format that can easily processed by using any other types of software.

MATLAB matrixes are exported as a CSV file for purposes like data interchange, data backup, data presentation, and more. Hence, if you are working with MATLAB, then it is a common practice to export a matrix as a comma-separated values file or CSV file.

What is a CSV File?

A CSV file or Comma Separated Values file is a type of text file that is used to store tabular data in the form of comma-separated values. In a CSV file, there are several rows and columns, where the values within a row are separated by a comma (,).

The important characteristics of a CSV file is that it does not store the information about the data types. In this file, all the data is considered as a plain text. The interpretation of this text data entirely depends upon the software.

Another key characteristic of a CSV file is that it can be opened and processed with a wide range of software applications, it does not support any kind of formatting features, and the first row of data is considered as the header row.

The following is an example of a simple CSV file:

Course,Fee,Duration
MATLAB,499,9 Hours
C++,259,6 Hours
Python,799,15 Hours

In this CSV file, the first row is the header row containing field names. The rest rows contain the data entries with values separated by a delimiter comma (,).

After getting a brief overview of CSV file, let us now discuss how we can export a matrix as a CSV file in MATLAB.

Export Matrix as a CSV File in MATLAB

In MATLAB, exporting a matrix as a CSV file is a very simple process. For this, we can use a MATLAB built-in function "csvwrite". However, there is another built-in function "writematrix" that can be used to export a matrix as a CSV file. But this function is available in MATLAB R2019b and later version only.

The step−by−step process of exporting a matrix as a CSV file is described below.

Method (1) − Export Matrix as CSV File using "csvwrite" Function:

To export a matrix as a CSV file using the "csvwrite" function, you have to follow the following steps:

Step (1) − Create or read your matrix in MATLAB.

Step (2) − Call the "csvwrite" function to save the matrix in your current MATLAB directory.

Syntax

csvwrite(matrix.csv, matrix);

Here, "matrix.csv" is the filename, and "matrix" is the matrix created in MATLAB.

Example

To understand these steps, consider the following MATLAB example.

% MATLAB code to export matrix as CSV file
% Create a sample matrix
mat = [10 20 30; 40 50 60; 20, 40, 60];

% Specify a file name
filename = 'mat.csv';

% Use csvwrite function to export the matrix as a CSV file
csvwrite(filename, mat);

% Display a confirmation message
fprintf('Your matrix has been successfully exported as "%s".
', filename);

Output

Your matrix has been successfully exported as "mat.csv".

Let us now understand another method of exporting a matrix as a CSV file.

Method (2) − Export Matrix as CSV File using "writematrix" Function:

The following steps are involved in exporting a matrix as a CSV file using the "writematrix" function.

Step (1) − Create or read the input matrix.

Step (2) − Specify a file name for the CSV file.

Step (3) − Specify the delimiter. It is an optional step, as the default delimiter is comma (,).

Step (4) − Export the matrix as a CSV file by calling the "writematrix" function.

Syntax

writematrix(matrix, filename, 'Delimiter', delimiter);

Here, "matrix" is the matrix created in MATLAB, filename is the name of the CSV file, delimiter is the symbol used to separate values in the CSV file.

Example

Let us now take an example to practically understand how to implement these steps in MATLAB.

% MATLAB code to export matrix as CSV file
% Create a sample matrix
mat = [10 20 30; 40 50 60; 70 80 90];

% Specify a file name of the CSV file
filename = 'mat.csv';

% Specify the delimiter
delim = ',';	% default is comma

% Call the writematrix function to export the matrix as CSV file
writematrix(mat, filename, 'Delimiter', delim);

% Display a confirmation message
fprintf('Your matrix has been successfully exported as "%s".
', filename);

Output

Your matrix has been successfully exported as "mat.csv".

Conclusion

In conclusion, MATLAB provides built-in functions that help us to export a matrix in the form of a CSV (Comma Separated Values) file. In MATLAB, this is a straightforward process, as described in this article. In this tutorial, I have included two examples in MATLAB to demonstrate the process of exporting a matrix as a CSV file. You can try these MATLAB codes with different matrices.

Updated on: 10-Oct-2023

900 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements