Real Technology Tools

Xlsread Tutorial: Extract Data from Excel in MATLAB

This article is a quick tutorial on using the xlsread MATLAB command to import data from an Excel file into MATLAB. Specifically, you’ll learn to:

How to Extract Data from Excel in MATLAB

Xlsread Basic Syntax

Here’s the basic syntax to import data from Excel into MATLAB:

[numbers text textAndNumbers] = xlsread(excelFileName)

Xlsread MATLAB Example

First, let’s look at some details about how this works through an actual example. Let’s say I have the following Excel file named “excelFile.xlsx:”

Example of reading an Excel file using xlsread

Then, applying the previous basic syntax to read it, we get:

excelFileName = 'excelFile.xlsx';
[numbers text textAndNumbers] = xlsread(excelFileName);

So, let’s examine the outputs of the xlsread MATLAB command:

As you can see, the three outputs are not of the same data type:

Remove Irrelevant Content from Your Extract

Import Data from a Specific Sheet

What If the Content Is in Another Sheet?

Let’s use the previous Excel file “excelFile.xlsx” with the content on a sheet named “Sheet1” (which is the default name of a sheet if you create an Excel file).
Moreover, if you add a sheet to the Excel file, the name of the sheet will be “Sheet2.” So, let’s do that and move the first sheet (with the previous content) to the right of the second sheet and save the file:

Then, we’ll apply the xlsread MATLAB command as we did previously:

excelFileName = 'excelFile.xlsx';
[numbers text textAndNumbers] = xlsread(excelFileName);

By default, xlsread will read the sheet located at the very left of the Excel file, namely “Sheet2.”


Since “Sheet2” is empty, it makes sense that the extracted content is empty. So, how do you read “Sheet1”?

Finding a Sheet

First, there are two ways to specify the sheet to read using the xlsread MATLAB command:

  1. Using the number of the sheet:
    [numbers text textAndNumbers] = xlsread(excelFileName, sheetNumber);

    The number of the sheet is “2” here because we want to read the second sheet (counting from the left). So, giving the “sheetNumber” variable the value “1” is equivalent to not using the second argument of the xlsread MATLAB command. Here’s how to extract the desired sheet:

    sheetNumber = 2; % second sheet counting from the left
    excelFileName  = 'excelFile.xlsx';
    [numbers text textAndNumbers] = xlsread(excelFileName, sheetNumber);
  2. Using the name of the sheet:
    [numbers text textAndNumbers] = xlsread(excelFileName, sheetName);

    The sheet name in that example is “Sheet1,” so you can use it as the second argument if you want to extract the second sheet:

    sheetName = 'Sheet1';
    excelFileName  = 'excelFile.xlsx';
    [numbers text textAndNumbers] = xlsread(excelFileName, sheetName);

In both cases, if the sheet number or the sheet name you’re referring to doesn’t exist, you’ll get the following error:

Specific Problems and Solutions

Ask the User to Select an Excel File

Using the uigetfile MATLAB command, you can ask the user to find and select the desired excel file to be read:

[fileName, pathName] = uigetfile({'*.xlsx';'*.xls'}, 'Choose an Excel file');

You can then use the “fileName” and the “pathName” (respectively the name of the selected file and its location) to read your Excel file.
There are 2 ways of doing that:

Be Careful About the Format of the Cells You’re Extracting

First, if the numbers contained in your Excel file are formatted as strings, you’ll get a cell array when extracting. For example, by adding a single quotation mark to the left of every number in the Excel file “excelFile.xlsx” that we’ve used previously, we are formatting them as strings:And if we import the data from the Excel file now, we get:

Since the numbers have been formatted as strings in the Excel file, there are no numbers anymore, the “numbers” variable is empty and the “text” and “textAndNumbers” variables have become identical.

The Empty Cell Issue

If you have empty cells in your Excel file before the first row, the xlsread MATLAB command will get rid of them. This is a problem if you want to write the content back to the Excel file (e.g. to modify a value) because you won’t be able to know where to write it.

Unfortunately, there is no easy way to get that information using xlsread. If you have this issue, you can refer to this article for a workaround: https://realtechnologytools.com/matlab-row-number/

Key takeaways:

You can find more information about the xlsread MATLAB function in the MathWorks documentation: https://fr.mathworks.com/help/matlab/ref/xlsread.html

🌲 If you want to learn more about the practical tips that helped me stop wasting time doing mindless work, such as working with Excel files and writing reports, I wrote a small reference book about it:

👉 www.amazon.com/dp/B08L7FM1BB

Exit mobile version