How to Read XLSX in MATLAB

Ammar Ali Feb 02, 2024
  1. Read Excel XLSX File Using readtable() Function in MATLAB
  2. Read Excel XLSX File Using readmatrix() Function in MATLAB
  3. Read Excel XLSX File Using readcell() Function in MATLAB
How to Read XLSX in MATLAB

In this tutorial, we will discuss how to read the excel file using the readtable(), readmatrix(), and readcell() function in MATLAB.

Read Excel XLSX File Using readtable() Function in MATLAB

You can read the excel file of any extension and many other types of files using the readtable() function. This function reads the file data and saves it in a table that contains variables on each column. If the excel 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.xlsx');

In the above code, we are reading a file with the name fileName that has the extension xlsx. You can change the name of the file and the extension according to the given file. If you defined row names for each row in the excel file, you could import them using the property ReadRowNames, and they will not be saved under a variable. See the code below.

data = readtable('fileName.xlsx','ReadRowNames',true);

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:3 , 1:2);

The above code will print the first three rows and the first two columns. If you want to read specific range data from the excel file, you can define the range of the columns using the property Range. See the example code below.

data = readtable('fileName.xlsx',...
    'Range','C1:E7',...
    'ReadVariableNames',false)

In the above code, we specified a range from column C1 to column E7 present in a spreadsheet. Make sure to check your data range from the spreadsheet 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 excel file with variables or names of each column, you can use this property. In this way, you will know which variables are being imported and which are not. If you don’t know the variable’s name, their types, and range of the data, you can use the detectImportOptions() function to detect the properties of a spreadsheet. See the example code below.

import_options = detectImportOptions('fileName.xlsx')

Using this function, you will get a lot of useful information about your excel file. Check this link for more information about the readtable() function.

Read Excel XLSX File Using readmatrix() Function in MATLAB

If you have numeric data saved in an excel file, you can use the readmatrix() function to read the data into a matrix. See the example code below.

MyMatrix = readmatrix('fileName.xlsx')

You have to specify the file name along with its extension inside the readmatrix() function to read the file. You can also use the detectImportOptions() function to detect and set the import options. For example, let’s detect and select the sheet number, variable names, and data range of the spreadsheet. See the code below.

Iopts = detectImportOptions('fileName.xlsx')
Iopts.Sheet = '2007';
Iopts.SelectedVariableNames = [1:5]; 
Iopts.DataRange = '2:11';
data = readmatrix('fileName.xlsx',Iopts);

You can change these properties according to your excel file. You can also define the range of the column to import using the Range property. See the example code below.

data = readmatrix('fileName.xlsx','Range','B1:C10');

Make sure to check your data range from the spreadsheet before using the Range property. Check this link for more information about the readmatrix() function.

Read Excel XLSX File Using readcell() Function in MATLAB

If you have cell data saved in an excel file, you can use the readcell() function to read the data into a cell. See the example code below.

MyCell = readcell('fileName.xlsx')

You have to specify the file name along with its extension inside the readcell() function to read the file. You can also use the detectImportOptions() function to detect and set the import options. See the example code below.

Iopts = detectImportOptions('fileName.xlsx');
Iopts.SelectedVariableNames = {'Variable1','Variable2'};
MyCell = readcell('fileName.xlsx',Iopts);

In the above code, we have selected two variables from which we want to read the data. You can define the variable names according to your excel file and many other properties. You can also define the sheet number and range of the data using the Sheet and Range properties. See the example code below.

MyCell = readcell('fileName.xlsx','Sheet','100','Range','A1:C10')

Make sure to check your excel file before setting any property. Check this link for more information about the readcell() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Excel