Read CSV Files in MATLAB: Choosing the Right Import Function

A practical review of how MATLAB reads CSV and TSV files with readtable, readmatrix and readcell, what the import changes in your data, and when to keep plain text.

Reading a CSV file in MATLAB converts delimited plain text into an in-memory structure such as a table, a numeric matrix or a cell array. The original file stays the same. The data you work with, however, is an interpretation of that file. This review covers which import route fits which purpose, what MATLAB changes during import, and when it makes more sense to keep the raw text.

Is MATLAB's CSV import suitable for your use?

For analysis, MATLAB's import functions are usually the right choice. They split fields, detect headers, and convert columns to numbers, text or dates, so you can compute on the data right away. The decisive tradeoff is that this convenience depends on automatic type detection. MATLAB can silently reshape values it misreads. Common examples are identifiers with leading zeros, codes that look numeric, ambiguous date formats and inconsistent missing-value markers.

Plain text is preferable in several situations:
• You need to preserve every value exactly as written, for auditing or archiving.
• You are only passing the file to another system unchanged.
• You need to inspect malformed rows before deciding how to parse them.
In those cases, read the file as text or as strings first, then convert deliberately.

The main import functions and how they differ

Function | Returns | Best suited for | Main caveat
readtable | Table with named, typed columns | Mixed data with headers | Header names may be altered to valid identifiers; column types are guessed
readmatrix | Numeric (or other homogeneous) array | All-numeric data | Non-numeric fields typically become NaN in a numeric result
readcell | Cell array | Irregular or mixed content you want to inspect raw | Less convenient for computation
textscan with fopen | Cell array per format specifier | Full manual control over parsing | More code; you must specify the format yourself
csvread / dlmread | Numeric matrix | Legacy scripts | Older numeric-only functions; MathWorks documentation recommends the newer functions instead

The simplest starting point for most files with a header row is readtable:

T = readtable('data.csv');
head(T)
summary(T)

For purely numeric files, readmatrix returns an array directly:

M = readmatrix('measurements.csv');

What reading a CSV file actually changes

Import does not rewrite characters in the file. Instead, MATLAB applies several interpretation steps, and each one can change how a value is represented in memory.

  • Field splitting: MATLAB detects the delimiter and splits each line into fields. Quoted fields that contain commas should stay intact, but malformed quoting can shift columns.
  • Type conversion: Text such as 00123 can become the number 123, which removes the leading zeros. Long numeric identifiers stored as double can lose precision beyond what double-precision floating point can represent exactly.
  • Date interpretation: Columns that look like dates may become datetime values. A day/month versus month/day ambiguity can produce valid but wrong dates.
  • Missing values: Empty fields and markers such as NA may become NaN, missing strings or empty values, depending on column type and options.
  • Header names: Column headers containing spaces or symbols may be modified into valid MATLAB identifiers. The 'VariableNamingRule','preserve' option keeps the original names in releases that support it.
  • Text encoding: If the file's encoding is detected incorrectly, accented letters and other non-ASCII characters can appear garbled. Specifying the encoding explicitly avoids relying on detection.

To control these steps, generate import options, inspect them, and override what MATLAB guessed:

opts = detectImportOptions('data.csv');
opts = setvartype(opts, 'ID', 'string');      % keep leading zeros
opts.VariableNamingRule = 'preserve';          % keep original headers
T = readtable('data.csv', opts);

If you suspect an encoding problem, state the encoding explicitly:

T = readtable('data.csv', 'Encoding', 'UTF-8');

Reading TSV and other delimited files

The same functions handle tab-separated and other delimited text. If the file extension is not .csv or .txt, tell MATLAB that the file is text and name the delimiter:

T = readtable('data.tsv', 'FileType', 'text', 'Delimiter', '\t');
T2 = readtable('data.txt', 'Delimiter', ';');

Semicolon-delimited files often use a comma as the decimal separator. Check that numeric columns came through as numbers rather than text, and adjust the import options if they did not.

Will the data stay usable after export or sharing?

Once data is in MATLAB, the table or array is meaningful only inside MATLAB. To share results, write them back to delimited text with writetable, writematrix or writecell. A round trip does not guarantee identical text. Numbers may be formatted differently, dates follow the datetime display format, and values that were converted on import, such as stripped leading zeros, cannot be recovered on export.

writetable(T, 'cleaned.csv');

Exported CSV remains plain text, so it can be opened in text editors, searched and processed by other tools. Spreadsheet applications may apply their own type conversions when they open the file, so a file that is correct on disk can still display altered values there. When a downstream reader or system needs exact source values, give them the original file or export identifiers as quoted text, and verify the result in that specific destination.

For accessibility, a CSV file with clear header names and one value per cell is generally easier for screen readers and other tools to navigate than formatted or merged layouts. No destination-specific or assistive-technology test results were supplied for this review. Confirm behavior in the tools your readers actually use.

Practical recommendation

Use readtable for most CSV and TSV files with headers, and readmatrix when the content is entirely numeric. For anything where exact values matter, such as IDs, codes, mixed-format dates or non-English text, run detectImportOptions first and set column types and encoding explicitly instead of trusting automatic detection. Keep the original plain-text file as the reference copy, because import is an interpretation and export does not undo it.

Gaps in this review

This review uses no benchmark measurements, version-specific test results or sample-file outputs. It describes documented, generally known behavior of MATLAB's import functions. Option names and defaults can differ between MATLAB releases, so check the documentation for your installed version, and test with a representative sample of your own files before relying on the results.