How to Import Excel Data into MATLAB

Cody Schneider8 min read

Moving your data from an Excel spreadsheet into MATLAB is the first step for powerful analysis and visualization, but it can sometimes feel a bit tricky. This guide will walk you through the most common and effective methods, from a simple point-and-click interface to more powerful command-based functions. We'll cover the readtable function, the Import Tool, and the classic xlsread command so you can pick the best approach for your specific task.

Before You Begin: Preparing Your Excel File

A little preparation in Excel can save you a lot of headaches in MATLAB. Before you start the import process, take a moment to check your spreadsheet for a few key things. A clean, well-structured file will make the import process much smoother.

Key Preparation Steps:

  • Consistent Column Headers: Make sure your data has a single row of headers at the very top. MATLAB's import tools work best when they can easily identify column names. Avoid merged cells or multi-row headers.
  • Tidy Up Your Data: Remove any charts, images, pivot tables, or macros from the sheet you plan to import. Your goal is to have a simple, rectangular block of data with headers in the first row and data in the subsequent rows.
  • Check Data Types: Ensure each column contains only one type of data (e.g., all numbers, all text, or all dates). Mixed data types within a single column, like having "N/A" text in a numeric column, can cause import issues or result in your numbers being treated as text. MATLAB will represent empty Excel cells as NaN (Not-a-Number) for numeric columns, which is the correct way to handle missing data.
  • Use Named Ranges (Optional but Recommended): If you only need to import a specific block of data, naming that range in Excel is a great practice. For example, instead of remembering you need A1:F50, you can simply name that range "SalesData." This makes your MATLAB code much cleaner and easier to read. To do this in Excel, highlight the cells and type the name into the Name Box to the left of the formula bar.

Method 1: Using MATLAB’s Interactive Import Tool

The easiest and most intuitive way to get started is with MATLAB’s built-in Import Tool. This graphical user interface (GUI) lets you visually select your data, choose how it's imported, and even generate the MATLAB code to automate the process next time.

This method is perfect when you're importing a file for the first time or if you're still getting comfortable with MATLAB commands.

Step-by-Step Guide to the Import Tool

  1. Open the Import Tool: In the MATLAB top ribbon, navigate to the HOME tab. In the Variable section, click on the Import Data icon. This will open a file browser.
  2. Select Your Excel File: Browse to the location of your Excel file, select it, and click Open. MATLAB will open the Import Tool interface and display a preview of your spreadsheet.
  3. Select the Data Range: The tool automatically highlights what it thinks is your main data table. If this selection is incorrect, you can click and drag over the cells you want to import. You can also specify the range directly in the "Range" input box at the top (e.g., A1:G100). If your Excel file has multiple sheets, you can select the correct one from the dropdown menu on the left.
  4. Choose the Output Type: This is a crucial step. In the Output Type section of the Import Tool's ribbon, you can decide how MATLAB should store your data. The most common options are:
  5. Handle Headers and Missing Data: The Import Tool is pretty smart about auto-detecting your column headers and how to treat empty cells. Look at the variable names it has chosen (shown at the top of each column in the preview) and confirm they match your headers. Under the "Unimportable Cells" section, you can change the rule for empty or non-numeric cells, but the default "Replace with NaN" is usually the best option for numeric columns.
  6. Import Your Data: Once you're satisfied with the preview and your settings, click the green checkmark labeled Import Selection. Close the Import Tool, and you will see the new variable(s) in your MATLAB workspace, ready for analysis. Pro-Tip: Before importing, click the dropdown under Import Selection and select Generate Script or Generate Function. MATLAB will automatically write the code required to perform the exact import you just configured. This is fantastic for tasks you'll need to repeat later - just save the script and run it whenever your Excel file updates.

Method 2: Using the readtable Function (Recommended for Scripts)

For reproducible analysis and scripting, using a MATLAB function is the way to go. The most modern and flexible function for this purpose is readtable. It's designed to import tabular data directly into a MATLAB table, automatically detecting headers, data types, and creating a clean, easy-to-use variable.

Basic Usage

At its simplest, you just need to provide the file name. Make sure the Excel file is in your current MATLAB folder, or provide the full path to the file.

myData = readtable('sales_data.xlsx')

This single line of code reads sales_data.xlsx and creates a table in your workspace called myData. MATLAB uses the first row of the Excel sheet as the variable names for the table columns.

Advanced readtable Options

Often, you'll need more control over the import process. readtable has several optional arguments (name-value pairs) that let you customize its behavior.

Specifying the Sheet

If your data isn't on the first sheet, specify which one to read from using the 'Sheet' option.

% Import data from a sheet named 'Q3_2023_Data'
myData = readtable('sales_data.xlsx', 'Sheet', 'Q3_2023_Data')

% Or you can use the sheet index (the 2nd sheet)
myData = readtable('sales_data.xlsx', 'Sheet', 2)

Specifying the Data Range

If you only want to import a specific block of cells, use the 'Range' option. This is where using a Named Range in Excel pays off.

% Import using a standard Excel range
myData = readtable('sales_data.xlsx', 'Range', 'B2:F51')

% Import using a named range from Excel called 'SalesData'
myData = readtable('sales_data.xlsx', 'Range', 'SalesData')

Handling Headers and Data Assumptions

MATLAB usually guesses correctly, but you can explicitly tell it how to handle your file.

% Specify extra options using 'opts'
opts = detectImportOptions('sales_data.xlsx')
opts.DataRange = 'A2', % Tells MATLAB that data starts at A2
opts.VariableNamesRange = 'A1', % Tells MATLAB headers are in row 1
myData = readtable('sales_data.xlsx', opts)

The detectImportOptions function is extremely powerful. It lets you pre-configure everything about the import process, including data types for each column, how to handle missing values, date formats, and more. This gives you precise control, especially for complex or messy files.

Method 3: The Classic xlsread Function

Before readtable existed, xlsread was the primary function for importing Excel data. While MathWorks now recommends using readtable for tabular data, you may still see xlsread in older scripts or find it useful in certain situations, particularly when dealing with mixed numeric and text data that isn't cleanly structured.

The key difference is that xlsread returns multiple outputs - one for numbers, one for text, and one for the raw, unprocessed data.

How xlsread Works

A typical call to xlsread looks like this:

[num, txt, raw] = xlsread('sales_data.xlsx')

Here’s what each output variable contains:

  • num: A numeric matrix containing only the numeric data from the spreadsheet. Any cells containing text will be represented as NaN.
  • txt: A cell array containing only the text data from the spreadsheet. Any cells with numbers will be represented as empty strings.
  • raw: A cell array containing all the data exactly as it was in the sheet, with numbers as numbers and text as text. This can be useful, but you'll have to manually process this array to separate and convert data types.

When to Use xlsread?

xlsread is less convenient than readtable for standard data tables. The separate outputs for numbers and text can mean extra work to line them up again. However, it can be useful for:

  • legacy code maintenance.
  • importing worksheets that don't have a simple, rectangular table structure.
  • when you need fine-grained control over extracting distinct numeric and text content separately.

Like readtable, you can also specify the sheet and range as additional arguments:

% Read from the third sheet in a specific range
[num, txt, raw] = xlsread('project_data.xlsx', 3, 'A5:E25')

Choosing the Right Method for Your Task

With three methods, how do you decide which to use?

  • For beginners or one-off tasks, start with the Import Tool. It's visual, intuitive, and lets you generate code to learn from.
  • For any kind of scripting or repeatable analysis, use readtable. It is the modern, powerful, and recommended approach for handling tabular data correctly.
  • Use xlsread only if you're working with older code or if you have a non-standard Excel sheet layout that doesn't fit a tabular format.

Final Thoughts

Importing data from Excel into MATLAB is a fundamental skill that opens the door to serious analysis. By preparing your spreadsheet, you can smoothly bring in your data using the interactive Import Tool for quick tasks, or more robustly with the readtable function for automated and reproducible workflows.

Just as these tools are designed to remove the friction of getting scientific data ready for analysis, we designed Graphed to do the same for business data. A lot of marketing and sales teams still spend their Mondays manually downloading CSVs and Excel files from Shopify, Google Analytics, and various ad platforms. We built Graphed to automate that entire process, letting you connect your sources once and create real-time, professional dashboards just by asking questions in plain English - no more manual imports required.

Related Articles

How to Connect Facebook to Google Data Studio: The Complete Guide for 2026

Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.

Appsflyer vs Mixpanel​: Complete 2026 Comparison Guide

The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.