textscan
Read formatted data from a character vector, string or file.
📝Syntax
C = textscan(chr, format)
C = textscan(fid, format)
C = textscan(__, Name, Value)
[C, position] = textscan(__)
📥Input Arguments
Parameter Description
chr a character vector or string scalar to read from.
fid a file identifier returned by fopen. Data is read from the current position to the end of the file.
format a character vector describing the conversion specifiers applied to each field.
Name, Value one or more name/value option pairs.
📤Output Arguments
Parameter Description
C a cell array with one cell per conversion specifier.
position the number of characters read when scanning stopped.
📄Description

textscan reads formatted data and returns a cell array C. Each cell holds one output column collected across all repetitions of the format string, since the format is cycled over the whole input.

Numeric conversion specifiers produce column vectors, while %s, %q and %[...] produce cell arrays of character vectors.

Supported conversion specifiers:

%d signed integer (int32), %u unsigned integer (uint32), %f floating point (double), %s whitespace or delimiter separated text, %q optionally double quoted text, %c a fixed number of characters, %[...] and %[^...] character set scanning.

A field width may be given (for example %5d or %3s). A conversion prefixed with * (for example %*d) is read but not stored. A size suffix selects the numeric class (%d8, %d16, %d32, %d64, %u8 and %f32). Literal text between specifiers must be matched in the input.

Supported name/value options:

Delimiter a character vector, or a cell array of character vectors, used to separate fields.

HeaderLines the number of leading lines to skip.

CollectOutput when true, consecutive columns of the same class are concatenated into a single array.

EmptyValue the numeric value used for empty numeric fields.

Whitespace the characters treated as whitespace.

MultipleDelimsAsOne when true, consecutive delimiters are treated as a single delimiter.

CommentStyle a comment marker, or a start and end pair, whose text is ignored.

TreatAsEmpty text values that are treated as empty numeric fields.

EndOfLine accepted for compatibility; end of line characters are always treated as whitespace separators.

💡Examples
C = textscan('1 2 3', '%d')
C = textscan('a,b,c', '%s', 'Delimiter', ',');
C{1}
C = textscan('name:42', '%[^:]:%d')
🔗See Also
sscanffscanffopen
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub