textread

Reads formatted text-file data into separate variables.

Category: Data Import and Analysis > Data Import and Export

Syntax

[A, B, ...] = textread(filename, format)

Reads columns according to the format, one output per conversion.

[A, B, ...] = textread(filename, format, Name, Value)

Sets options such as 'delimiter' and 'headerlines'.

Description

textread is an older function; textscan, readtable and readmatrix replace it. It is still convenient when you want each column in its own variable, with text columns as cell arrays of character vectors.

Input arguments

format
Conversions such as %s, %d, %f, one per column.
'delimiter'
Field separator.
'headerlines'
Number of lines to skip at the start.

Output arguments

A, B, ...
One column vector or cell array per conversion.

Example

Split a file into columns

fid = fopen('tx.txt', 'w'); fprintf(fid, 'a 1\nb 2\nc 3\n'); fclose(fid);
[name, val] = textread('tx.txt', '%s %f')
fid = fopen('q.csv', 'w'); fprintf(fid, 'x,y\n1,2\n3,4\n'); fclose(fid);
[x, y] = textread('q.csv', '%f %f', 'delimiter', ',', 'headerlines', 1)
Output
name = 3×1 cell

    {'a'}
    {'b'}
    {'c'}
val = 3×1

     1
     2
     3
x = 2×1

     1
     3
y = 2×1

     2
     4

See also

textscan readtable readmatrix fscanf