How to Read CSV File in Matlab
-
Read CSV File Using
readtable()
Function in MATLAB -
Read CSV File Using
readmatrix()
Function in MATLAB -
Read CSV File Using
readcell()
Function in MATLAB
In this tutorial, we will discuss how to read a CSV file using the readtable()
, readmatrix()
, and readcell()
functions in MATLAB.
Read CSV File Using readtable()
Function in MATLAB
You can read a CSV file using the readtable()
function. This function reads the file data and saves it in a table that contains variables on each column. If the CSV file does not contain variables on each column, the readtable()
function will give them a default variable name starting from var1
and so on. For example, see the code below.
data = readtable('fileName.csv');
In the above code, we read a file with the name fileName
with the extension .csv
. If you want to display a portion of the available data, you can do it using the object data
in which the data is stored. See the code below.
data(1:5 , 1:6);
The above code will print the first five rows and the first five columns. If you want to read specific range data from the CSV file, you can define the range of the columns using the property Range
. See the example code below.
data = readtable('fileName.csv','Range','A1:C7');
In the above code, we specified a range from column A1
to column C7
present in a spreadsheet. Make sure to check your data range from the CSV file before using the range. The property ReadVariableNames
is used to specify if you want to read the first row as variables or not. If you have saved the CSV file with variables or names of each column, you can use this property. In this way, you will know which variables are imported and which are not. If you don’t know the variable’s name, its types, and range of the data, you can use the detectImportOptions()
function to detect the properties of the CSV file. See the example code below.
import_options = detectImportOptions('fileName.csv')
Using this function, you will get a lot of useful information about your CSV file. Check this link for more information about the readtable()
function.
Read CSV File Using readmatrix()
Function in MATLAB
If you have numeric data saved in a CSV file, you can use the readmatrix()
function to read the data into a matrix. See the example code below.
MyMatrix = readmatrix('fileName.csv')
You have to specify the file name and its extension inside the readmatrix()
function to read the file. You can also use the detectImportOptions()
function to detect and set the import options. You can change the import properties according to your CSV file. You can also define the range of the column to import using the Range
property. Make sure to check your data range from the CSV file before using the Range
property. Check this link for more information about the readmatrix()
function.
Read CSV File Using readcell()
Function in MATLAB
If you have cell data saved in a CSV file, you can use the readcell()
function to read the data into a cell. See the example code below.
MyCell = readcell('fileName.csv');
You have to specify the file name and its extension inside the readcell()
function to read the file. You can also use the detectImportOptions()
function to detect and set the import options. You can change the import properties according to your CSV file. You can also define the range of the data using the Range
property. See the example code below.
MyCell = readcell('fileName.csv','Range','A1:C10');
Make sure to check your CSV file before setting any property. Check this link for more information about the readcell()
function.