Matlab

How to Read a CSV File in MATLAB Using csvread() Function

CSV files are the widely used files for interchanging data between systems. These files have a standardized format that is compact and easy. These files have compatibility with many applications including MATLAB.

If you are working with MATLAB, you can utilize the csvread() function to read data from the CSV files. Read this guide where you will find a detailed guide on how to read CSV files in MATLAB using the csvread() function.

How to Read CSV Files in MATLAB Using the csvread() Function?

The csvread() is a built-in function in MATLAB that is used for reading and displaying data from CSV files. This function accepts the file name with .csv extension and reads and displays the data from that file on the command window.

Syntax
The csvread() function follows several syntaxes, which can be used in different ways, as given below:

M = csvread(filename)
M = csvread(filename,R1,C1)
M = csvread(filename,R1,C1,[R1 C1 R2 C2])

Here:

The function M = csvread(filename) yields to read the comma-separated values formatted files in an array M. Keep in mind that these files should contain only numeric data.

The function M = csvread(filename, R1, C1) yields to read the data from the CSV file ranges from R1 row and C1 column.

The function M = csvread(filename, R1, C1,[R1 C1 R2 C2]) yields read data from the specific range between the rows R1, R2, and the columns C1, C2.

Examples
Follow the given examples to understand how to read data from the CSV files in MATLAB.

Example 1: How to Read CSV Files in MATLAB Using csvread(filename) Function

In this example, we first create a CSV file using the writematrix() function and then read that file using the csvread() function in MATLAB.

writematrix(rand(10,3),"myfile.csv", "Delimiter",",");
A = csvread('myfile.csv')

Note: The “Delimiter” argument in the code specifies the delimiter that will be used to separate the column in the CSV file.

Example 2: How to Read CSV Files in MATLAB Using csvread(filename, R1, C1) Function

This example creates a CSV file using the writematrix() function and then reads data from the file in the specified range using the csvread() function.

writematrix(rand(10,3),"myfile.csv","Delimiter",",")
A = csvread('myfile.csv', 4, 0)

Example 3: How to Read CSV Files in MATLAB Using csvread(filename, R1, C1,[R1 C1 R2 C2]) Function?

This MATLAB code creates a CSV file and uses the csvread() function to read data from the file in the given range.

writematrix(rand(10,3),"myfile.csv","Delimiter",",")
A = csvread('myfile.csv', 4, 0,[4 0 5 1])

Conclusion

Reading data from the CSV file is an important task and MATLAB provides a built-in csvread() function to quickly read the CSV file. You can specify the argument inside the function according to your choice by setting the range of rows and columns. This guide has provided you with easy-to-learn examples to help you in using the csvread() function to read your important CSV file while programming and use them in your code.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.