MATLAB Read CSV File: Practical Guide for Importing Data

A comprehensive guide to reading CSV files in MATLAB using readtable and readmatrix. Learn how to handle headers, delimiters, missing values, and large files with practical code examples, import options, and best practices.

MyDataTables
MyDataTables Team
·5 min read
MATLAB CSV Import - MyDataTables
Photo by This_is_Engineeringvia Pixabay
Quick AnswerDefinition

MATLAB reads CSV files primarily with readtable and readmatrix. Use readtable to obtain a table with headers and mixed data types; use readmatrix when all data are numeric to get a numeric matrix. Start with readtable('data.csv','Delimiter',',','ReadVariableNames',true) and, for numeric-only data, readmatrix('data.csv','Delimiter',','). For missing cells, configure Import Options (TreatAsMissing/MissingValue) to preserve data integrity.

MATLAB CSV Reading: Core methods

MATLAB provides two primary ways to bring CSV data into memory: readtable and readmatrix. The readtable function returns a table, which preserves column headers as variable names and keeps mixed data types in columns. The readmatrix function returns a numeric matrix when the CSV contains only numeric data, which is often faster for numerical computations. Both functions accept a Delimiter option, with comma as default for standard CSVs, but they also support semicolons or other delimiters. In practice, you typically start with readtable for data frames and once you know your numeric subset, you may extract numerical arrays with table2array or readmatrix. Here are simple patterns to start:

MATLAB
T = readtable('data.csv','Delimiter',',','ReadVariableNames',true); M = readmatrix('data.csv','Delimiter',',');

To inspect the imported data, you can use:

MATLAB
summary(T) size(M)

If the CSV contains missing fields, you can configure how MATLAB handles them via Import Options. This approach helps prevent misinterpreting empty cells as numeric zeros and keeps data integrity intact.

Import Options and Headers

The detectImportOptions function can automatically infer variable types and names. You can adjust options before reading the file. For example:

MATLAB
filename = 'customers.csv'; opts = detectImportOptions(filename); T = readtable(filename, opts);

Then customizing for missing values and whitespace:

MATLAB
opts = setvaropts(opts, {'Age','Income'}, 'TreatAsMissing', {'NA','-'} ); T = readtable(filename, opts);

You can also preserve whitespace in strings:

MATLAB
opts = setvaropts(opts, 'Name', 'WhitespaceRule', 'preserve'); T = readtable(filename, opts);

Using detectImportOptions gives a structured approach that reduces trial-and-error when dealing with varied CSV layouts, especially when headers are inconsistent or data types vary across files. Experiment with different options and validate a subset of rows to ensure robust parsing.

Handling Delimiters and Quoted Text

If the file uses a nonstandard delimiter, specify it; MATLAB will also handle quoted strings by default. Examples:

MATLAB
D = 'data_semicolon.csv'; T1 = readtable(D,'Delimiter',';','ReadVariableNames',true);

For string-type data, you can control the TextType:

MATLAB
opts = detectImportOptions(D); opts = setvaropts(opts, 'Name', 'TextType', 'string'); T2 = readtable(D, opts);

Quoting within fields is handled by MATLAB automatically, but you can customize how whitespace and quotes are preserved to ensure fields like product descriptions stay intact during import.

Reading Large CSV Files Efficiently

Large datasets can exceed available RAM. MATLAB offers datastore and tall arrays to stream data. Start by creating a tabularTextDatastore or a datastore and process data in chunks:

MATLAB
ds = tabularTextDatastore('large.csv','Delimiter',','); ds.ReadSize = 1e4; T = tall(ds); head(T) % preview a few rows

To perform computations without loading everything, operate on tall objects and use gather to fetch results:

MATLAB
meanVal = mean(T.NumericCol); result = gather(meanVal);

For truly gigantic files, consider chunked processing and writing intermediate results to disk, then concatenating results at the end.

Handling Missing Data during Import

Missing values are common in CSVs. Use Import Options to define missing-value rules:

MATLAB
filename = 'data.csv'; opts = detectImportOptions(filename); opts = setvaropts(opts, opts.VariableNames, 'TreatAsMissing', {'NA','N/A',''}); T = readtable(filename, opts);

After import, you can clean or fill missing values:

MATLAB
T = fillmissing(T, 'constant', 0);

For numeric columns, you can specify what character sequences count as missing and convert strings to numeric carefully using str2double as needed.

Converting to MATLAB Types and Basic Cleaning

Often you need to coerce columns to the right types before analysis. The Import Options help, but you may also post-process:

MATLAB
% Convert a numeric column stored as text to double if iscell(T.Price) T.Price = str2double(T.Price); end % Build a numeric matrix for fast math A = table2array(T(:, {'Price','Quantity','Discount'}));

Accessing data by name is convenient:

MATLAB
avgDiscount = mean(T.Discount);

This section shows how to bridge between table-based workflows and numeric arrays.

Practical Workflow: End-to-End Example

Here's a compact end-to-end example that reads a CSV, applies import options, summarizes, and saves results:

MATLAB
filename = 'sales.csv'; opts = detectImportOptions(filename); opts = setvaropts(opts, {'Date'}, 'InputFormat', 'yyyy-MM-dd'); T = readtable(filename, opts); summary(T); writetable(T, 'sales_clean.csv');

If you want to persist the results for reuse, save the table to a MAT-file:

MATLAB
save('sales_import.mat','T');

This workflow demonstrates how to move from raw CSV to a clean, analyzable MATLAB table.

Common Pitfalls and Best Practices

  • Always validate your import with a sample of rows before processing the full dataset to catch format mismatches early.
  • Use detectImportOptions to tailor behavior to each CSV instead of hard-coding formats; this reduces errors across files.
  • Delimiter mismatches are a common cause of corrupted data; verify the delimiter for nonstandard CSVs and ensure downstream consumers read the same format.
  • For large files, prefer streaming with tall tables or datastores and write intermediate results to disk to avoid memory spikes.
  • Data cleaning often requires post-import steps: convert string columns to numeric, detect and handle unexpected nulls, and document the import options used for reproducibility.

Steps

Estimated time: 30-60 minutes

  1. 1

    Install and verify MATLAB

    Install MATLAB (R2020a or later) and verify the version via ver in the Command Window. Create a project folder and ensure CSV files are accessible from the script's working directory.

    Tip: Keep your project folder organized with relative paths to data.
  2. 2

    Create a read script

    Write a MATLAB script that uses readtable and readmatrix to import a sample CSV. Start by reading headers and a numeric subset to verify data types.

    Tip: Use detectImportOptions to tailor the import for each file.
  3. 3

    Tune import options

    Create an Import Options object, adjust TreatAsMissing and Whitespace rules, and test with different Delimiter values to support nonstandard CSVs.

    Tip: Always validate a subset of rows first.
  4. 4

    Handle large files

    If the CSV is large, switch to a tabularTextDatastore or tall table to stream data in chunks, avoiding full memory load.

    Tip: Monitor memory usage with memory() and MATLAB's workspace tools.
  5. 5

    Clean and convert data

    Convert string columns to numeric if needed and extract numeric matrices for computation using table2array.

    Tip: Use try/catch blocks for robust parsing.
  6. 6

    Save and reuse

    Save the imported data to a MAT-file for faster reloads and reuse a consistent file path and versioning.

    Tip: Document file provenance and import options for reproducibility.
Pro Tip: Prefer readtable to preserve headers and mixed types; use readmatrix for numeric-only data.
Warning: Always validate imports; misread data types can lead to incorrect analyses.
Note: DetectImportOptions helps adapt to different CSV layouts without rewriting code.

Prerequisites

Required

Optional

  • Sample CSV file (data.csv)
    Optional
  • Text editor to edit MATLAB code
    Optional

Commands

ActionCommand
Run a MATLAB script from the command lineIn this article, you can also run scripts from the MATLAB editor; for CLI, use the MATLAB -batch or -r options.None

People Also Ask

What is the difference between readtable and readmatrix?

readtable returns a MATLAB table with named variables (headers). readmatrix returns a numeric array when the file contains numeric data. Choose based on your downstream workflow.

readtable gives you labeled columns you can access by name, while readmatrix returns a numeric matrix for fast numeric work.

How do I handle missing values during CSV import?

Use detectImportOptions to build an Import Options object, then set 'TreatAsMissing' or 'MissingValue' for relevant variables. Read with the customized options to preserve data integrity.

Configure missing data handling in Import Options to avoid misreading blanks as zeros.

Can MATLAB read CSVs with different delimiters?

Yes. Specify 'Delimiter' in readtable/readmatrix or adjust the Import Options accordingly to support semicolon, tab, or other delimiters.

MATLAB can read nonstandard CSVs by setting the delimiter option when importing.

What should I do if the CSV is too large to fit in memory?

Use tabularTextDatastore and tall tables to process data in chunks; then aggregate or save results instead of loading everything at once.

Process data in chunks to avoid memory limits.

How can I import dates reliably?

Provide an exact InputFormat for date columns via Import Options and use datetime or string types as appropriate.

Specify the date format to ensure correct parsing.

Main Points

  • Use readtable to preserve headers
  • Choose readmatrix for numeric-only data
  • Customize import with Import Options
  • Process large files with datastore or tall tables
  • Always validate results before computation

Related Articles