The Wayback Machine - https://web.archive.org/web/20240708055915/https://www.geeksforgeeks.org/how-to-export-a-matrix-as-a-csv-file-in-matlab/
Open In App

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

Last Updated : 18 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A CSV file – Comma Separated File, as the name suggests, is a file that stores data delimited by a ‘ , ‘. In MATLAB, there is a simple way of exporting a matrix to a csv file. The writematrix() function is used to write a matrix into any desired file type. The syntax is

writematrix(<matrix_name>, “filename.extension”);

This will create a new file in the current directory with the matrix stored in it. Let us see the same for csv files with various examples. Now, the following is a 2×5 matrix, and we will export it to a csv file named matrix.csv.

Example 1:

Matlab




% MATLAB code for 2x5 matrix, and we will 
% export it to a csv file named matrix.csv.matrix
    m = [1 2 3 4 5;10 9 8 7 6];
  
% Exporting to csv file
    writematrix(m, 'matrix.csv')


Output:

The created csv file: 

 

Let’s export a 4×5 matrix with floating point data to csv file.

Example 2:

Matlab




% MATLAB Code for Exporting a 4x5 matrix
% with floating point data to csv file.
% Matrix
    m = [
     1.1 1.2 1.3 1.4 1.5;
     2.1 2.2 2.3 2.4 2.5;
     3.1 3.2 3.3 3.4 3.5;
     4.1 4.2 4.3 4.4 4.5
     ];
  
% Exporting to csv file
    writematrix(m, 'matrix.csv')


Output:

The new csv file would be:

 



Similar Reads

How To Import Data from .CSV File With Numeric Values and Texts Into MATLAB Workspace?
A .csv file is a comma-separated file, in which consecutive data columns are separated by commas and the end of a line is the default EOL character. While dealing with small data, it is common to use a .csv file for storing it. In this article, we shall discuss how to import .csv files, with numeric data and their text headers as the column variabl
2 min read
How to Read Image File or Complex Image File in MATLAB?
MATLAB is a programming and numeric computing platform used by millions of engineers and scientists to analyze data, develop algorithms, and create models. For Image Reading in MATLAB, we use the image processing toolbox. In this ToolBox, there are many methods such as imread(), imshow() etc. imshow(I) : shows the grayscale image I in a figure.imsh
2 min read
MATLAB - Read Words in a File in Reverse Order
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It allows matrix manipulations, plotting of functions, implementation of algorithms, and creation of user interfaces Suppose we are given a text file containing the following words "I STUDY FROM GEEKS FOR GEEKS". Now, if we want to read the
2 min read
How to Append Data to a File in MATLAB?
Appending data to a text file means adding data to a file that already exists in the storage. print() function is used to write/append data to a file in MATLAB. It writes formatted text to a file based on the format string provided to it. The format of the output/input is determined by the formatting specifier. A format specifier is used to format
4 min read
How to Read Text File Backwards Using MATLAB?
Prerequisites: Write Data to Text Files in MATLAB Sometimes for some specific use case, it is required for us to read the file backward. i.e. The file should be read from EOF (End of file Marker) to the beginning of the file in reverse order. In this article we would learn how to read a file in backward using MATLAB. For demonstration we would be u
3 min read
Function with Multiple Inputs in a Script File in MATLAB
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. In this article, we will see a function with multiple inputs in a script file in MATLAB. Steps:The steps below can be used to build a function with multiple inputs in a MATLAB script file: Launch a fresh function in MATLAB.To create the func
2 min read
How to Select Random Rows from a Matrix in MATLAB?
A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array. Syntax:matrix_name(i,j)where, i is the row number, and J is the column number which is to be indexed. Example 1: C/C++ Code % MATLAB code for select % random matrix elements mat = magic(5); mat(
2 min read
Page-wise matrix multiplication in MATLAB
Page-wise matrix multiplication is multiplying two N-D arrays along each dimension or page of two arrays. Matlab allows users to calculate page-wise multiplication using pagemtimes(). Different syntax of pagemtimes() method are: Z = pagemtimes(X,Y)Z = pagemtimes(X,transpX,Y,transpY)Z = pagemtimes(X,Y)It returns the page-wise multiplication of N-D a
2 min read
Matlab - Matrix
A Matrix is a two-dimensional array of elements. In MATLAB, the matrix is created by assigning the array elements that are delimited by spaces or commas and using semicolons to mark the end of each row. Now let's have a glance at some examples to understand it better. Syntax: a = [elements; elements] Example: Creating a Matrix C/C++ Code % MATLAB p
4 min read
How to swap elements in the matrix in MATLAB?
In this article, we will see the swapping of elements into a matrix in MATLAB. Different methods are illustrated below: Method 1: By changing elements of rows and columns In this method, we are simply changing the elements of particular rows and columns in the specified rows and columns respectively. Example 1: C/C++ Code % MATLAB code for 2*2 matr
3 min read