Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
To read a CSV file in MATLAB, call T = readtable("file.csv") when the file has a header row or mixed column types, and A = readmatrix("file.csv") when every value is numeric. Both functions detect the delimiter and header lines automatically. The older csvread still works, but MathWorks’ documentation states it is not recommended and points to readmatrix instead. The sections below show each call, how to read what it returns, and how to fix the import problems the documentation describes.
Every function, option name and default in this guide comes from the MathWorks MATLAB documentation (sources listed at the end, read 2026-11-12). The data table after the body compares the CSV-capable import functions side by side.
What it is used for in research
CSV (comma-separated values) is the most common plain-text exchange format for instrument exports, survey downloads, repository datasets and spreadsheet saves. Reading one into MATLAB turns the file into a variable you can summarise, plot, model or clean. The choice of import function decides what type of variable you get back, and that choice affects every later step: a table keeps column names and mixed types together, a numeric matrix does not, and a cell array holds anything.
MathWorks’ guide to importing text files lists the purpose of each function: readtable for mixed-type tabular data into a table, readmatrix for uniform data (all of the same type), readcell for non-uniform data where each column has a different type, readtimetable when each row has a timestamp, readlines for plain text lines into a string array, and textscan for low-level control, described as not necessary for most workflows. If your file is a column-oriented dataset with named columns, you almost certainly want readtable. For a broader view of working with the resulting table variable, see the CASRAI guide to the MATLAB table data type.
How to install or access it
The import functions covered here (readtable, readmatrix, readcell, detectImportOptions, csvread) are documented in the core MATLAB reference at mathworks.com/help/matlab. Obtain MATLAB only from MathWorks or through your institution’s licence administrator, and use the documentation for the release you have installed, because function pages note the release in which a function or option was introduced. This guide does not cover licensing or pricing. For a general orientation to the product, see what MATLAB is used for; for Linux installations see MATLAB on Linux. If you do not use MATLAB at all, the free alternatives table later on this page covers other tools that read CSV files.
MATLAB also offers an interactive route. The MathWorks guide to importing text files lists the Import Tool, a user interface for choosing how a file is parsed, as an alternative to typing commands. The step-by-step section below uses commands because they are repeatable and can be saved in a script alongside your analysis.
Step-by-step: read a CSV file
The example uses a hypothetical file named measurements.csv in MATLAB’s current folder, whose first line holds column names. It is an illustration of the documented syntax, not a published dataset.
1. Read a headed or mixed-type file with readtable
- Put the file in the current folder, or use a full path in the file name.
- Run:
T = readtable("measurements.csv") - MATLAB returns a table with one variable per column. The documentation’s own example,
T = readtable("myCsvTable.csv"), states that the first line of the file supplies the variable names.
The files readtable treats as delimited text are those with the extensions .txt, .dat and .csv. The same function also reads spreadsheet formats such as .xls and .xlsx, so the same call works if a collaborator sends Excel instead of CSV.
2. Read an all-numeric file with readmatrix
- Run:
A = readmatrix("measurements.csv") - MATLAB creates an array by reading column-oriented data and, by default, imports the data as a numeric array.
- Use the
OutputTypeoption if you want another type. The documentation lists double, single, the integer types, char, string, datetime, duration and categorical.
readmatrix auto-detects the number of header lines (the NumHeaderLines option); override it with a positive integer if the detection is wrong. To read only part of the file, use the Range option, which accepts forms such as 'A5', [5 1], 'C2:N15' and [2 3 15 13].
3. Read mixed or irregular columns with readcell
Run C = readcell("measurements.csv"). This returns a cell array and suits non-uniform data in which each column has a different type. The documentation says that for delimited text, readcell converts empty fields to missing values. Its text-parsing options include Delimiter, CommentStyle, ConsecutiveDelimitersRule, TextType ("char" or "string") and Encoding.
4. Import only some columns, or control missing data, with detectImportOptions
- Detect the options:
opts = detectImportOptions("measurements.csv"); - Select columns by name. The documentation’s example selects two variables from
airlinesmall.csv:opts.SelectedVariableNames = ["TaxiIn","TaxiOut"]; - Read with the options:
T = readtable("measurements.csv", opts);
To see what MATLAB detected before importing, call preview(opts), which the documentation shows displaying a preview of the file. The same opts object can be passed to readmatrix and readcell. For repeated imports, MathWorks notes that the options object gives more control, better performance and reusability.
Set options that change detection as name-value pairs when you call detectImportOptions, for example detectImportOptions("airlinesmall.csv",'MissingRule','omitrow'). The documentation advises against changing properties by dot notation after detection, because the other options are not re-detected.
5. Reading with csvread (legacy)
The documentation for csvread says: “csvread is not recommended. Use readmatrix instead.” It adds that there are no plans to remove csvread, and that it has been in this status since R2019a. The call M = csvread(filename) is equivalent to M = readmatrix(filename), with these stated limits: the file must contain only numeric values, and csvread also accepts a start row and column (csvread(filename,R1,C1)) and a range (csvread(filename,R1,C1,[R1 C1 R2 C2])). MathWorks lists the advantages of readmatrix as better cross-platform support and performance, automatic detection of format and data types, and import options for errors and missing data. Use readmatrix for new code, and keep csvread in mind only when you maintain older scripts.
Output and how to read it
What you get depends on the function:
readtablegives a table: one variable per column, with the names taken from the first line when the file has them. Refer to a column by its variable name rather than a position, which keeps analysis scripts readable when columns are reordered.readmatrixgives a numeric array by default. There are no column names, so keep a note of which column holds which measurement.readcellgives a cell array, with empty fields converted tomissingvalues for delimited text.
Variable names are modified by default
For text files, readtable uses the VariableNamingRule value "modify" by default, which converts column names that are not valid MATLAB identifiers (for example names containing spaces) into valid ones. If you need the original headers exactly as they appear in the file, pass VariableNamingRule as "preserve". Check this before you merge the result with other data by variable name.
Missing values
For detectImportOptions, the MissingRule default is "fill"; the other values are "error", "omitrow" and "omitvar". With "fill", missing entries are replaced by a fill value that you set per variable with setvaropts. Decide deliberately which rule fits your analysis, because dropping rows ("omitrow") and filling values change sample size and results differently. In readtable, the related options TreatAsMissing and MissingRule let you control which text values count as missing.
Troubleshooting
| Symptom | Cause and fix (from the MathWorks documentation) |
|---|---|
| Column names in the table differ from the file’s headers | VariableNamingRule defaults to "modify" for text files; set it to "preserve" to keep the original names. |
| Import fails or misaligns rows | For readmatrix and readcell, all lines in the text file must have the same number of delimiters. Fix ragged rows in the source file. |
| Header lines are miscounted | NumHeaderLines is auto-detected; override it with a positive integer in readmatrix, or use ReadVariableNames in readtable to say whether the first row holds names. |
| Semicolon-delimited file with decimal commas | The Delimiter option is auto-detected when not specified; set it explicitly if detection fails. readmatrix has a DecimalSeparator option (for example ','). |
| Only some columns are needed | Use detectImportOptions and SelectedVariableNames, then pass opts to the read function. |
| Options set after detection seem ignored | Setting properties by dot notation after detectImportOptions does not re-detect the other options; pass them as name-value pairs to detectImportOptions instead. |
| Garbled text or accented characters | Set the Encoding option. When unspecified, MATLAB detects the encoding automatically. |
csvread errors on a file with text |
csvread accepts only numeric values. Use readtable or readcell. |
| A file has no extension or a non-standard one | Set FileType to "text" (or "spreadsheet"), as documented for readcell. |
Free alternatives
If you do not have a MATLAB licence, other tools read CSV files with equivalent one-line calls. The table lists the function each tool’s own documentation describes.
| Tool | Function | What the documentation says |
|---|---|---|
| Python (pandas) | pandas.read_csv |
“Read a comma-separated values (csv) file into DataFrame.” The default separator is a comma and the header row is inferred. |
| R | read.csv |
Returns a data frame; the defaults are header = TRUE and sep = ",". See the CASRAI guide to reading CSV files into R. |
| GNU Octave | csvread, dlmread |
csvread reads a CSV file into a matrix and handles only numeric data; it is equivalent to dlmread with a comma separator. |
Octave’s documentation describes the two numeric functions above. Its documentation page on simple file I/O does not describe a readtable function, so do not assume MATLAB code using tables will run unchanged in Octave. If your next step is time-series work in Python, see the CASRAI guide to resampling data with pandas.
FAQ
What is the best way to read a CSV file in MATLAB?
For most research files, readtable, because it keeps column names and mixed types in one table. Use readmatrix when all values are numeric and you only need an array.
Is csvread removed from MATLAB?
No. The documentation says it is not recommended and that there are no plans to remove it. Use readmatrix in new code.
How do I read a CSV file with a header row?
Use readtable; the first line supplies the variable names. With readmatrix, header lines are auto-detected and skipped, and you can override the count with NumHeaderLines.
How do I read only some columns?
Create import options with detectImportOptions, set SelectedVariableNames, and pass the options to readtable.
Can readtable read Excel files too?
Yes. The same function reads spreadsheet formats such as .xls and .xlsx as well as .txt, .dat and .csv text files.
Sources
- MathWorks, readtable (read 2026-11-12).
- MathWorks, readmatrix (read 2026-11-12).
- MathWorks, readcell (read 2026-11-12).
- MathWorks, csvread (read 2026-11-12).
- MathWorks, detectImportOptions (read 2026-11-12).
- MathWorks, Ways to import text files (read 2026-11-12).
- pandas documentation, pandas.read_csv; R documentation, read.table / read.csv; GNU Octave manual, Simple File I/O (all read 2026-11-12).
Data: MATLAB functions for importing CSV files: return type, suitable data and status
Compares the MATLAB text-import functions that can read CSV files, by what they return, the kind of data each is intended for, and their documented status. Compiled from the MathWorks MATLAB documentation pages for each function and the guide to importing text files.
| Function | Returns | Intended for | Documented status |
|---|---|---|---|
| readtable | table | Mixed-type tabular data; reads .txt, .dat, .csv as delimited text and also spreadsheets | Listed in the import guide |
| readmatrix | numeric array (default) | Uniform data, all of the same type | Listed in the import guide; the documented replacement for csvread |
| readcell | cell array | Non-uniform data, each column a different type | Listed in the import guide |
| readtimetable | timetable | Data where each row has a timestamp | Listed in the import guide |
| readlines | string array | Plain text lines | Listed in the import guide |
| textscan | cell array of columns | Low-level control; described as not necessary for most workflows | Listed in the import guide |
| csvread | numeric matrix | File must contain only numeric values | Not recommended since R2019a; no plans to remove |
Sources
- MathWorks: Ways to import text files (accessed 2026-11-12)
- MathWorks: readtable (accessed 2026-11-12)
- MathWorks: readmatrix (accessed 2026-11-12)
- MathWorks: readcell (accessed 2026-11-12)
- MathWorks: csvread (accessed 2026-11-12)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







