Reading an Excel file in MATLAB is a quick way to import tabular data—whether it’s financial records, survey results, or test scores—into variables for further analysis. With just a few lines of code, you can load entire worksheets or specific sheets, handle headers, and even convert data types. If your file is corrupted, has formatting issues, or isn’t in the standard `.xls`/`.xlsx` format, you’ll need extra steps. Below, I walk you through the exact steps, common pitfalls, and what to check when it doesn’t work.
What You'll Need
- A computer running MATLAB (any version since R2007b, which added native Excel support)
- The file you want to import (must be a valid Excel file: `.xls`, `.xlsx`, or `.xlsm`)
- A supported Excel version (MATLAB reads `.xlsx` natively; older `.xls` files may require the
xlrdorxlswritelibraries, though they’re rarely needed for import) - If working on older MATLAB versions (pre-R2014a), ensure you have the Excel Add-in installed via
addon install excel(unlikely for new users)
Step-by-Step
-
Open MATLAB.
Ensure you have an active session: click "Start" > "MATLAB" (or press
Ctrl+Nto open a new workspace). If you’re using a newer version of MATLAB (R2014a or later), Excel integration is built-in. Older versions may prompt for the Excel Add-in. -
Choose the Excel file.
Navigate to the folder containing your file using MATLAB’s
cdcommand:cd('path_to_your_folder')(Replace the path with your actual folder location. On Windows, use double backslashes or forward slashes; on macOS/Linux, forward slashes work.) Then type:filePath = 'your_file.xlsx';Replaceyour_file.xlsxwith your filename. MATLAB will now reference this path in memory. -
Load the entire sheet into a variable.
Use the
readmatrixfunction (for `.xlsx` files) orreadtable(for all formats) to read the data:data = readmatrix(filePath);This imports all columns and rows as a numeric array. If your file has headers, MATLAB will use them as column names by default. Alternative for older files: For `.xls` files, use:data = xlsread(filePath);(This works in MATLAB versions before R2014a.) -
Select a specific sheet.
If your file has multiple sheets, specify which one to read:
data = readmatrix(filePath, 'Sheet', 'Sheet1');Replace'Sheet1'with the sheet name or index (e.g.,'Sheet2'or'1'for the second sheet). Forreadtable, use:data = readtable(filePath, 'Sheet', 'Sheet1', 'ReadVariableNames', true);The'ReadVariableNames'flag ensures headers are preserved as column names. -
Skip optional formatting.
To skip formatting (e.g., currency symbols, dates) and treat cells as numeric strings, use:
data = readmatrix(filePath, 'Range', 'A1:Z1000');Rangelets you specify subsets of the sheet (e.g., columns A-Z, rows 1-1000). Forreadtable, add:data = readtable(filePath, 'Range', 'A1:Z1000', 'ReadVariableNames', true, 'VariableNames', {'col1', 'col2'});to rename or exclude columns. -
Verify the loaded data.
MATLAB will display the data in the Command Window. Type:
disp(data)or visualize it with:disp(whos)to check variable size and memory usage. If you encounter errors likeError reading file, check if the file path is correct or if the file is corrupted.
Common Mistakes
-
File path errors.
MATLAB will throw errors like
File not foundif the path is misspelled or the file doesn’t exist. Double-check: - The filename (case-sensitive on Windows/Linux; case-insensitive on macOS). - The folder structure (use forward slashes/or double backslashes\\). - File extensions (`.xls` vs `.xlsx`). If unsure, tryreadmatrix('file.xls'). -
Corrupted or locked files.
Excel may lock files (e.g., from macros or concurrent edits). Try:
- Saving the file as a plain
.xlsxwithout macros. - Closing all Excel instances before opening MATLAB. - Using thereadtablemethod (often more forgiving). -
Header mismatches.
If headers don’t align with column names, MATLAB may assign generic labels like
Variables. Fix with:data.Properties.VariableNames = {'col1', 'col2'};after loading, or use'ReadVariableNames', trueinreadtable. -
Old MATLAB versions.
Pre-R2014a MATLAB lacks native `.xlsx` support. Install the
xlrdpackage via:addon install xlrdThen usereadmatrix(filePath, 'FileType', 'xls').
Tips
-
Batch processing.
Load multiple files with a loop:
files = dir('*.xlsx');for k = 1:length(files)data{k} = readmatrix(fullfile(pwd, files(k).name));endThis creates a cell array of datasets. -
Data type preservation.
Use
readtableinstead ofreadmatrixif your file contains dates, text, or mixed data types. Example:data = readtable(filePath, 'DataReading', 'UseTextDelimiter', ',', 'TextType', 'string');Adjust delimiters (e.g., comma, semicolon) as needed. -
Memory optimization.
For large files, use:
data = readmatrix(filePath, 'Range', 'A1:Z1000', 'NumHeaderLines', 1);to skip the first row if headers are in the data itself. -
Sheet naming conflicts.
If two sheets share the same name, append a suffix:
data = readmatrix(filePath, 'Sheet', 'Sheet1_Imported');or use indices (e.g.,'Sheet', 2for the second sheet).
Troubleshooting
-
General errors:
Start with the basics:
- Restart MATLAB to clear cached errors.
- Update MATLAB to the latest version (check
help version). - Test with a known-good file (e.g., a simple `.xlsx` with headers). If that did not work: Try reading aCSVfile first (simpler format):data = readmatrix('file.csv');If it works but Excel fails, the issue is likely file corruption. -
Excel Add-in prompt:
If you see a dialog asking to install the Excel Add-in, it’s an older MATLAB version. Either:
- Use
xlsreadfor `.xls` files, or - Update MATLAB to R2014a+ (Excel integration is built-in). -
Missing headers or columns:
Check for:
- Hidden columns/tabs in Excel (use
Ctrl+Shift+_to show). - Incorrect delimiter (e.g., semicolons in European files). Try:data = readtable(filePath, 'Delimiter', ';'); - Permission issues: On Windows, MATLAB may need write access to the folder. Try: - Running MATLAB as Administrator (Windows). - Checking folder permissions in macOS/Linux.
-
Mixed data types:
If MATLAB converts all data to numeric (e.g., dates become numbers), use:
data = readtable(filePath, 'DataReading', 'useColSpec');Then manually adjust types:data.Properties.VariableTypes = ['double'; 'datetime'];
Frequently Asked Questions
Can I read an Excel file into a MATLAB array with headers?
Yes, usereadmatrix with 'ReadVariableNames', true or readtable, which automatically assigns headers to columns. Example:
data = readtable(filePath, 'ReadVariableNames', true);
This creates a table with property Variables containing column names.
How do I read a specific sheet in an Excel file with multiple sheets?
Specify the sheet name or index:data = readmatrix(filePath, 'Sheet', 'Sheet1');
or by index:
data = readmatrix(filePath, 'Sheet', '2');
For readtable, use:
data = readtable(filePath, 'Sheet', 'Sheet1', 'ReadVariableNames', true);
What if my Excel file has macros or protected cells?
Save the file as a plain.xlsx (File > Export > Export Workbook) to remove macros. For protected cells, disable protection in Excel before saving:
File > Options > Proofing > Enable editing.
Why does MATLAB read my Excel file as text instead of numbers?
MATLAB may convert columns to text if they contain symbols (e.g., currency signs). Fix by: 1. Opening the file in Excel, removing symbols, then resaving. 2. Usingreadtable with explicit data types:
data = readtable(filePath, 'VariableTypes', repmat({'double'}, 1, numel(data.Properties.VariableNames)));
How can I read an Excel file with dates in columns?
Usereadtable or convert dates manually after loading:
data.Dates = datetime(data.Dates);
For readmatrix, dates appear as numbers (e.g., 18013) and require conversion:
data.Dates = datetime('ConvertFrom', 'Excel', data.Dates);
Why does MATLAB sometimes ignore columns or rows?
This can happen if: - The file has blank rows/columns (use'NumHeaderLines' or 'NumVariableLines').
- Hidden rows are present (enable visibility in Excel).
- The sheet has merged cells (check Excel’s merged cell settings).
Example command to skip hidden rows:
data = readtable(filePath, 'SkipRows', 10);