How to Export MATLAB Data to Excel

Cody Schneider

Moving your data from MATLAB’s powerful analysis environment into an Excel spreadsheet is an incredibly common and useful task. Whether you need to share results with a colleague, create a client-ready report, or simply use Excel’s familiar charting tools, knowing how to export your data correctly is essential. This guide will walk you through the most effective methods to export data from MATLAB to Excel, focusing on the two powerhouse functions: writematrix() and writetable().

First, a Quick Prep: Getting Your Data in Order

Before you can export anything, your data needs to be organized in a variable within the MATLAB workspace. For Excel exports, your data will almost always be in one of two formats: a matrix or a table.

  • A matrix is a numeric array, perfect for when you're working with raw numbers like sensor readings, financial data, or scientific measurements.

  • A table is more versatile, allowing you to store columns with different data types (like text, numbers, and logical values) under named headers, much like a spreadsheet.

For our examples, let's create a sample matrix and a sample table.

First, a simple numeric matrix representing quarterly sales data:

Next, a table containing mixed data about employees:

With our sample data ready, let's start exporting.

Method 1: Exporting Numeric Arrays with writematrix()

The writematrix() function is your go-to command for exporting purely numeric data (matrices, vectors, or arrays). It’s fast, simple, and perfect for when you just need to get your numbers into a spreadsheet without any complex formatting.

The Basic Export

At its simplest, you only need to provide the matrix you want to export and a filename for your new Excel file. MATLAB will handle the rest.

Let's export our salesMatrix to a file named 'quarterly_sales.xlsx':

When you run this code, MATLAB will create a new Excel file named quarterly_sales.xlsx in your current working folder. If you open it, you’ll see your matrix data placed neatly, starting in cell A1.

Adding Parameters for More Control

Sometimes, you need more control than just dumping data into a new file. You might want to specify which worksheet to use or even a specific cell range. You can do this by adding "Name-Value pairs" to the function.

Writing to a Specific Worksheet

By default, MATLAB writes to the first sheet in the Excel file. To specify a different sheet, use the 'Sheet' parameter. If the sheet doesn't exist, MATLAB will create it for you.

Writing to a Specific Cell Range

This is incredibly useful when you're adding data to an existing report or template. Use the 'Range' parameter to tell MATLAB exactly where to start writing the data. You only need to specify the top-left cell of the range.

Let’s say we want to place our sales data starting in cell C5 of the 'Q1-Data' sheet:

After running this command, your Excel file will now have the sales matrix data beginning in cell C5 on the "Q1-Data" sheet, leaving all other cells untouched.

Method 2: Exporting Mixed Data with writetable()

What if your data isn't just numbers? If you're working with a MATLAB table that contains text, dates, or other data types, writetable() is the function you need. Its biggest advantage is that it can automatically export the variable names from your table as column headers in Excel.

The Basic Export with Headers

Much like writematrix(), the basic syntax is straightforward. Let’s export our employeeTable to a file named 'employee_data.xlsx':

When you open this new Excel file, you’ll see the data from your table. Notice that the first row automatically contains the variable names from our MATLAB table: EmployeeID, Age, Department, and IsManager.

Advanced Options with writetable()

The writetable() function supports many of the same useful parameters as writematrix(), including 'Sheet' and 'Range'.

Let’s write our employee data to a sheet named "HR_Records", starting at cell B2:

Controlling the Headers

By default, writetable() includes the column headers. What if you don't want them? For example, you might be appending data to an existing spreadsheet table that already has headers. You can disable this by setting the 'WriteVariableNames' parameter to false.

Opening this file will show only the raw data from the table, starting in cell A1, without the header row.

Comparing writematrix() vs. writetable()

So, which one should you use? Here’s a quick cheat sheet to help you decide.

Use writematrix() when:

  • Your data is entirely numeric (a matrix, vector, or scalar).

  • You don't need to export column headers or are fine adding them manually in Excel.

  • You need the absolute simplest way to get numbers out of MATLAB.

Use writetable() when:

  • Your data is organized in a MATLAB table.

  • You have mixed data types (text, numbers, booleans, etc.).

  • You want to automatically export your column variable names as headers in Excel.

  • Your dataset represents a structured report or record log.

Common Issues and Troubleshooting Tips

Most of the time, exporting works flawlessly. But when it doesn't, here are a few common issues and how to fix them.

  • Error: "Unable to write file..." This is almost always a permissions issue. The most common cause is that you have the same Excel file you are trying to write to already open on your computer. Close the file in Excel and try running your MATLAB code again.

  • Data Looks "Weird" in Excel: Excel sometimes tries to be too helpful with its auto-formatting. For example, it might convert a long ID number into scientific notation (like 1.23E+10). This isn't a MATLAB export error. The solution is to select the affected columns in Excel and manually change the cell format to "Number" or "Text."

  • Overwriting Data On Accident: Be aware that both writematrix and writetable will overwrite any existing data in the cell range you specify. They will also overwrite an entire spreadsheet if you specify a sheet that already contains data without giving a range. If your workflow involves updating a file, be certain of your target range before running your script.

Final Thoughts

Connecting your analysis in MATLAB with the reporting power of Excel is valuable. Using writematrix() for your numeric arrays and writetable() for your structured tables gives you a complete toolkit for any data export scenario. Mastering these two functions will save you countless hours of manual copy-and-pasting.

While moving data to Excel is often a final step, the true goal is creating clear dashboards and actionable reports. That process of building charts and summaries in spreadsheets can be yet another time-consuming, manual task. To remove that friction, we built Graphed. We connect directly to your primary data sources, allowing you to ask questions in plain English and instantly get back real-time dashboards and reports. This way, you spend your time on insights, not on manual exporting and chart building.