readmatrix
Reads a numeric matrix from a text or spreadsheet file.
Category: Data Import and Analysis > Data Import and Export
Syntax
A = readmatrix(filename)
Reads the file, detecting the delimiter and skipping header lines of text.
A = readmatrix(filename, Name, Value)
Sets options such as 'NumHeaderLines', 'Delimiter' or, for spreadsheets, 'Sheet'.
A = readmatrix(filename, opts)
Uses an import options object.
Description
readmatrix is the standard way to load a table of numbers. It looks at the file, drops a leading header made of text, and returns a double matrix. Fields it cannot read as numbers, such as empty cells or the word NaN, are stored as NaN.
When the file mixes text and numbers, use readtable or readcell so the text is kept.
Input arguments
filename- Text, CSV or spreadsheet file name.
'NumHeaderLines'- Number of lines to skip at the top.
'Delimiter'- Field separator; detected automatically when omitted.
'Sheet'- Sheet name or number in a spreadsheet file.
Output arguments
A- Numeric matrix. Empty or non-numeric fields become
NaN.
Example
Header and missing values
fid = fopen('h.csv', 'w'); fprintf(fid, 't,v\n1,NaN\n2,5\n,7\n'); fclose(fid);
A = readmatrix('h.csv')
B = readmatrix('h.csv', 'NumHeaderLines', 1);
isequaln(A, B)
A = 3×2
1 NaN
2 5
NaN 7
ans = logical
1
Notes
The 'Range' option and 'OutputType', 'string' are not honored by MatLivre: the first returns more than the requested block and the second still returns numbers.