HOWTO · MATLAB
How to Read CSV File in Matlab
Choose readtable(), readmatrix(), or readcell() to import CSV data in MATLAB.
On this page
Choose the MATLAB importer by the shape of the result you need: readtable() creates a table with named variables, readmatrix() returns numeric data as a matrix, and readcell() returns the imported values in a cell array. Each function accepts a CSV filename, and each can read a selected range. The examples below explain when to use each result and how to inspect import settings with detectImportOptions().
Read CSV data into a table with readtable()
Use readtable() for tabular data whose columns may have different types, such as names alongside measurements. It uses the first row for variable names by default. When the file has no header row, MATLAB supplies default names; set ReadVariableNames to false to prevent the first data row from being treated as headers.
For a numeric CSV, GNU Octave can read the data after skipping its header row:
csvPath = [tempname(), '.csv'];
file = fopen(csvPath, 'w');
fprintf(file, 'year,sales\n2024,18\n2025,23\n');
fclose(file);
values = dlmread(csvPath, ',', 1, 0);
disp(values);
delete(csvPath);
In MATLAB, the corresponding table import is data = readtable('sales.csv'). Inspect a portion of the resulting table with data(1:5, 1:6): this selects five rows and six columns. To import only a file region, pass a range such as data = readtable('sales.csv', 'Range', 'A1:C7'). Confirm that the range includes the rows and columns you intend to read.
When the file’s layout or column types are unclear, use opts = detectImportOptions('sales.csv') to inspect the detected import settings, then adjust options before reading. For example, set opts.ReadVariableNames = false when the first row contains data rather than headers, and pass the options to readtable('sales.csv', opts). See the MathWorks readtable() reference for supported options.
Read numeric CSV data into a matrix with readmatrix()
Use readmatrix() when the values you need are numeric and a regular matrix is convenient for calculations. For example, measurements = readmatrix('measurements.csv') imports the numeric data. If the file contains a header row, MATLAB detects and handles import details based on the file; inspect the result and use import options when you need to control that behavior.
To read a particular region, use the Range name-value argument, for example measurements = readmatrix('measurements.csv', 'Range', 'B2:D8'). A range limits the imported rows and columns; it does not select a named variable as table indexing does. If you need to inspect or tune how MATLAB interprets the file, call detectImportOptions('measurements.csv') and pass the resulting options object to readmatrix. See the MathWorks readmatrix() reference for details.
Read CSV data into a cell array with readcell()
Use readcell() when a CSV contains a mixture of text and numeric values and you want to keep the imported entries together in a cell array. For example, values = readcell('inventory.csv') reads the file into cells, allowing individual entries to retain their imported types.
You can restrict the import to a region with values = readcell('inventory.csv', 'Range', 'A1:C10'). The selected range is expressed using spreadsheet-style row and column references. For a file whose import behavior needs adjustment, inspect settings with opts = detectImportOptions('inventory.csv'), then pass the options object to readcell('inventory.csv', opts). The exact available options depend on the importer; consult the MathWorks readcell() reference before changing them.