How to Read CSV Files in MATLAB: A Practical Guide
Learn to read CSV files in MATLAB using readtable, readmatrix, and ImportOptions. This step-by-step guide covers headers, delimiters, encodings, missing data, and performance tips for robust data analysis workflows.
To read a CSV in MATLAB, prefer readtable for mixed data or readmatrix for numeric data. Start by specifying the file path and choose delimiter if needed. Example: T = readtable('data.csv'); M = readmatrix('data.csv', 'Delimiter', ','); For headers, rowNames, and missing values, adjust options with detectImportOptions. Additionally, you can handle NA values using setvaropts or readmatrix options.
Understanding CSV inputs in MATLAB
CSV files are plain-text files that store tabular data in rows and columns. In MATLAB, reading CSVs is a common first step in data analysis, machine learning, and visualization workflows. The choice between readtable and readmatrix depends on your data: readtable returns a table with named variables (columns) and is ideal for mixed data types, while readmatrix returns a numeric matrix suitable for purely numeric data. MATLAB also offers readcell (to preserve heterogeneous cell content) and readvars for extracting specific columns. When you load data, you may encounter headers, missing values, or non-numeric columns; these situations are handled most robustly with ImportOptions and detectImportOptions. According to MyDataTables, establishing a reliable CSV-reading workflow reduces downstream errors and simplifies reproducible analysis.
Key ideas to keep in mind: the delimiter may be a comma, semicolon, or tab; header lines influence variable names; and MATLAB can infer types but often benefits from explicit options for stability.
Choosing readtable vs readmatrix vs readcell
- readtable: creates a MATLAB table with variable names derived from the CSV header and supports mixed data types (numeric, text, logical).
- readmatrix: loads numeric data into a numeric matrix; headers are discarded unless you also load them separately.
- readcell: loads everything as a cell array, preserving mixed types without automatic conversion.
When your CSV includes a header and you want to keep column labels for subsequent analysis, readtable is usually the best starting point. If you know your file is purely numeric and you need matrix operations, readmatrix can be more straightforward. For files with mixed types where you want to maintain text and numbers without conversion, readcell is a strong option.
Example: Quick read with default options
% Read a standard CSV with headers into a table
T = readtable('sales.csv');
% Read a numeric CSV into a matrix (headers ignored)
M = readmatrix('sales_numbers.csv');In many cases, you should customize how MATLAB imports data using ImportOptions to lock down types and special values.
Inspecting the loaded data
After loading, verify the dimensions and preview the data:
size(T) % for tables: returns [rows, cols]
head(T) % show first few rows of the table
summary(T) % summarize data types and basic statisticsIf the file is large, consider loading a subset to test your options before reading the entire dataset.
ImportOptions: robust control over reading CSVs
ImportOptions lets you tailor how MATLAB interprets each column. A typical workflow:
opts = detectImportOptions('data.csv');
% Customize options, e.g., treat empty fields as missing
opts = setvaropts(opts, opts.VariableNames, 'TreatAsMissing', {'NA','-',''});
% If you know a column is numeric, you can force its type
opts = setvaropts(opts, 'Amount', 'TrimNonNumeric', true);
T = readtable('data.csv', opts);Using detectImportOptions helps prevent misread columns and reduces post-processing steps.
Handling different delimiters and encodings
If your CSV uses a delimiter other than a comma, specify it explicitly:
opts = detectImportOptions('data_semicolon.csv');
opts.Delimiter = ';';
T = readtable('data_semicolon.csv', opts);For encoding differences (e.g., UTF-8 with BOM), you can set Encoding in readtable options:
opts = detectImportOptions('data_utf8.csv');
opts.Encoding = 'UTF-8';
T = readtable('data_utf8.csv', opts);These settings help avoid garbled text and misread numeric values.
Working with missing data and non-uniform columns
Missing values are common in CSVs; ImportOptions lets you designate how to treat them and which values count as missing. You can also choose to fill missing values later in MATLAB:
opts = detectImportOptions('data.csv');
opts = setvaropts(opts, 'Age', 'TreatAsMissing', {'NA',''});
T = readtable('data.csv', opts);
T.Age = fillmissing(T.Age, 'constant', NaN);Be mindful of non-uniform columns; readtable can handle ragged arrays when represented as cells, but readcell or careful options are safer for inconsistent rows.
Performance tips for large CSV files
For very large CSVs, readtable can be memory-intensive. Consider: reading in chunks with datastore, using tall arrays, or loading only needed columns:
opts = detectImportOptions('large_data.csv');
opts.SelectedVariableNames = {'Date','Sales','Region'};
T = readtable('large_data.csv', opts);If you only need a quick preview, load the first N rows with readtable and 'NumHeaderLines' adjustments, then scale up once you’ve validated the schema.
Converting between tables and arrays for downstream tasks
Sometimes, you’ll want a numeric matrix for computation and a table for labeled data. Convert as needed:
% From table to numeric matrix (only numeric columns)
numMatrix = table2array(T(:, varfun(@isnumeric, T, 'OutputFormat', 'uniform')));
% Back to table after math
results = array2table(numMatrix, 'VariableNames', T.Properties.VariableNames);Balancing label-rich data with numeric performance is a common data science workflow in MATLAB.
Practical workflow: end-to-end example
- Inspect the CSV structure (headers, delimiters).
- Load with readtable using detectImportOptions for robust typing.
- Validate the loaded data (dimensions, types).
- Handle missing values or convert types as needed.
- Use the resulting table for analysis, visualization, or export.
This end-to-end pattern minimizes surprises when you move from raw CSVs to insights.
Tools & Materials
- MATLAB software (R2018b or newer)(Ensure you have access to readtable, readmatrix, and ImportOptions APIs.)
- CSV file to test(Include headers and a realistic mix of data types if possible.)
- A text editor or IDE(Helpful for inspecting header names or editing a sample file.)
- Sample ImportOptions usage example(Keep a copy of a small CSV to test options before loading large files.)
Steps
Estimated time: 20-40 minutes
- 1
Identify CSV structure
Open the file in a text editor to check the delimiter, header row, and column names. This informs your choice of readtable vs readmatrix and helps configure ImportOptions.
Tip: If the header is multi-line, note which lines to skip before using detectImportOptions. - 2
Choose the read method
Decide between readtable (mixed data) and readmatrix (numeric data) based on the content of your CSV. For mixed data, plan to work with a table.
Tip: If you’re unsure about data types, start with readtable to inspect the result. - 3
Load with a simple example
Load a basic CSV using readtable to verify the structure and column names. This gives you a baseline to refine with options.
Tip: Verify the first few rows with head(T) to confirm correct parsing. - 4
Tune import options
Use detectImportOptions to tailor types, missing values, and delimiters. Apply setvaropts as needed to enforce numeric columns or treat values as missing.
Tip: Always test with a subset before applying to the full dataset. - 5
Handle delimiters and encoding
If your CSV uses a non-comma delimiter or a different encoding, adjust ImportOptions accordingly. This prevents misread values and garbled text.
Tip: Explicitly set Delimiter and Encoding to avoid platform-specific defaults. - 6
Validate and clean data
After loading, check for missing values, type mismatches, and outliers. Clean or transform data before analysis.
Tip: Use rough summaries (summary and varfun) to spot anomalies quickly. - 7
Consider big data considerations
For very large CSVs, consider loading in chunks or using datastore/tall arrays when possible. This avoids memory bottlenecks.
Tip: Filter columns you don’t need to minimize memory use. - 8
Document the workflow
Save the import options and the exact code as a script or function for reproducibility.
Tip: Add comments explaining why each option was chosen.
People Also Ask
What is the difference between readtable and readmatrix in MATLAB?
readtable returns a table with column names and mixed data types, ideal for labeled datasets. readmatrix loads a numeric matrix, discarding headers and non-numeric columns. Use ImportOptions to tailor behavior for any CSV.
readtable gives you labeled data in a table, handy for mixed content, while readmatrix gives you numbers in a matrix. Use ImportOptions to fine-tune the import.
How do I handle missing values during CSV import?
Use detectImportOptions to identify missing value patterns and setvaropts to define TreatAsMissing or EmptyField rules. After loading, you can fill or impute missing data as needed.
Identify missing values with ImportOptions, then fill or impute as needed.
Can MATLAB read CSV files with delimiters other than commas?
Yes. Set the Delimiter option in the ImportOptions to the appropriate character (e.g., ';' or '\t') before reading the file.
Yes, specify the delimiter in ImportOptions before loading.
How can I read only specific columns from a CSV?
Use SelectedVariableNames in ImportOptions or read the full table and then select the desired columns by name or index.
Load the file with ImportOptions and pick the columns you need by name.
What about non-ASCII encodings in CSVs?
Set the Encoding option in ImportOptions to match the file’s encoding (e.g., 'UTF-8' or 'ISO-8859-1') to avoid garbled characters.
Specify the file encoding in ImportOptions to prevent garbled text.
Is readtable still the recommended approach for CSVs in MATLAB?
Yes, for most CSVs containing mixed data, readtable remains the standard choice; use readmatrix for numeric-only data and readcell when you need to preserve all data types.
Readtable is generally recommended for mixed data; use readmatrix for numeric-only data.
Watch Video
Main Points
- Identify CSV structure before loading to choose the right function.
- Use ImportOptions for robust parsing and consistent types.
- Prefer readtable for mixed data and readmatrix for numeric matrices.
- Validate data after loading to catch missing values and type issues.

