Import CSV with MATLAB importdata

How MATLAB importdata turns CSV and TSV text into numbers and text, what can be lost on the way, and when readtable, readmatrix or readcell is the better choice.

importdata is one of the oldest general-purpose ways to pull a CSV file into MATLAB. It is quick to call and often works without options, but it changes your data on the way in: text fields become double-precision numbers where possible, header text is separated from values, and the shape of the result depends on what the function finds in the file. This review explains what that conversion does, where it keeps or loses meaning, and when a different reader or plain text is the safer choice.

Here, transformation means parsing delimited text into MATLAB variables. It does not mean visual effects such as reversed, mirrored or restyled characters. Those mechanisms do not apply to a CSV import.

Is importdata suitable for your CSV?

importdata is reasonable for small, regular files: one or a few header lines followed by purely numeric columns with a consistent delimiter. Its main benefit is convenience. One call with a filename usually returns a numeric matrix, or a structure holding numbers and header text.

The main tradeoff is control. importdata guesses the delimiter and header lines, converts numeric-looking fields to double, and gives you few options to override that per column. Files with mixed text and numeric columns, dates, identifiers with leading zeros, very long integer IDs, decimal commas or non-default encodings are better handled with readtable, readmatrix or readcell. Those functions let you set column types and parsing rules explicitly.

Plain text is preferable when the exact original characters matter, for example when auditing, diffing or re-exporting values exactly as received. In those cases keep the raw file. If you need the values inside MATLAB, read them as text (readcell, readlines or fileread) rather than letting them be converted to numbers.

What importdata actually changes

A CSV file is plain text. importdata splits each line on a delimiter, separates leading header lines, and converts fields that parse as numbers into double-precision values. The original characters are not stored. What MATLAB keeps is the numeric value that the text was interpreted as.

% Example file readings.csv (illustrative):
% id,temp,pressure
% 1,21.5,101.2
% 2,22.0,101.0

A = importdata('readings.csv', ',', 1);

% With a header line, A is a structure:
%   A.data       numeric matrix of the values
%   A.textdata   cell array of header text
%   A.colheaders column names, when a header row is detected

The output type depends on the file. If the file holds only numbers, importdata returns a plain numeric array. If it finds header text, it returns a structure, and code written for one form will break on the other. You can request the detected delimiter and header-line count as extra outputs to confirm what the function assumed:

[A, delimiterOut, headerlinesOut] = importdata('readings.csv');

Distinguish stored values from displayed values. By default the Command Window shows a short display format, so a value may appear rounded on screen while the stored double holds more digits. Changing the display (for example with format long) changes only how values are shown, not what is stored. The opposite case matters more: information lost during parsing, such as a leading zero, cannot be recovered by any display setting.

Content in the CSV | What importdata typically does | Risk to meaning
Plain numeric columns | Converted to double in a numeric matrix | Low for ordinary measurements
Header row | Placed in textdata and colheaders | Low, but the output becomes a structure instead of a matrix
Identifiers with leading zeros, such as postal codes | Parsed as numbers if they look numeric | Leading zeros are lost
Integers longer than about 15–16 digits | Stored as double | Values above 2^53 may not be exact
Dates and times | Not converted to datetime; treated as text if kept at all | Must be parsed separately
Text columns mixed with numbers | Split between textdata and data | Rows must be realigned manually; results vary with layout
Empty fields | Commonly filled with NaN in the numeric data | Blank and missing become indistinguishable from NaN in the source
Decimal commas | Not handled as decimals | Values may be split or misread

The table describes the function's general design, not test results for any particular MATLAB release. The exact handling of mixed or irregular rows can vary, so inspect the result before relying on it.

TSV and other delimiters

For tab-separated files, pass the tab delimiter and header-line count explicitly instead of relying on detection. Explicit settings make the result predictable when a file has few rows or ambiguous content.

A = importdata('data.tsv', '\t', 1);

Will the data stay usable after import, copying or sharing?

Inside MATLAB, the numeric part of an importdata result works well for calculations. The structure is less self-describing than a table, though. Column names live in a separate field from the values, and text columns must be matched to numeric rows by index. If you share the variable or pass it to other code, you need to document that layout.

importdata can also read from the clipboard with importdata('-pastespecial'). Pasted data follows the same parsing rules, so leading zeros, long IDs and dates face the same risks as a file import.

Writing the data back out with writematrix or writetable does not recreate the original text. Lost leading zeros, rounded long integers and dropped formatting stay lost, and the output may differ from the source in number formatting. If someone downstream needs the file exactly as received, share the original CSV rather than a re-export. That includes reviewers, other tools, and anyone reading it with assistive technology. The original plain text is the reference copy.

Alternatives with more control

MathWorks provides dedicated readers for tabular text. readtable returns a table with named, typed columns. readmatrix returns a numeric matrix. readcell returns a cell array that keeps text as text. Import options let you fix column types, delimiters, decimal separators and encoding before reading.

T = readtable('readings.csv');          % named, typed columns
M = readmatrix('readings.csv');         % numeric matrix
C = readcell('readings.csv');           % keeps text as text

% Keep an identifier column as text so leading zeros survive
opts = detectImportOptions('customers.csv');
opts = setvartype(opts, 'zip', 'string');
T = readtable('customers.csv', opts);

% Tab-separated file
T = readtable('data.tsv', 'FileType', 'text', 'Delimiter', '\t');

Checks before trusting an importdata result

  • Confirm the output type: numeric array or structure.
  • Compare the returned delimiter and header-line count with the actual file.
  • Check that the size of the numeric data matches the expected rows and columns.
  • Compare a few values against the raw file, especially IDs, codes and long integers.
  • Look for unexpected NaN values and decide whether they mean missing, blank or a parse failure.
  • Verify that text columns line up with their numeric rows.
  • Keep the original CSV as the reference copy.

Evidence gaps

No release-specific test results, sample files or measurements were available for this review. The behavior described above reflects the documented design of importdata and general properties of double-precision numbers. How importdata handles irregular files, such as mixed columns, quoted fields containing delimiters, or unusual encodings, should be confirmed on your own MATLAB version with a representative file before you depend on it.