Text Files

Delimited and formatted text files

Read and write numeric and non-numeric data in delimited and formatted text files, including .csv and .txt files. Import text file data interactively using the Import Tool. Import or export text data programmatically using the functions on this page.

Functions

expand all

Import ToolImport data from file

Basic Import and Export

readtableCreate table from file
writetableWrite table to file
readtimetableCreate timetable from file
writetimetableWrite timetable to file

Define Import Rules

detectImportOptionsCreate import options based on file content
delimitedTextImportOptionsImport options object for delimited text
fixedWidthImportOptionsImport options object for fixed-width text files
getvaroptsGet variable import options
setvaroptsSet variable import options
setvartypeSet variable data types
previewPreview eight rows from file using import options
readmatrixRead matrix from file
writematrixWrite a matrix to a file
readcellRead cell array from file
writecellWrite cell array to file
readvarsRead variables from file
textscanRead formatted data from text file or string
typeDisplay contents of file
filereadRead contents of file as text
readlinesRead lines of file as string array

Objects

tabularTextDatastoreDatastore for tabular text files

Topics

Read Tabular Data from Text Files

Import Text Files

Text files often contain a mix of numeric and text data as well as variable and row names, which is best represented in MATLAB® as a table.

Read Text File Data Using Import Tool

Preview tabular data from a text file or the clipboard and select data to import using the Import tool.

Import Mixed Data from Text File into Table

The best way to represent tabular data from text files in MATLAB is in a table since tables can store heterogeneous (a mix of numeric and text) data, as well as variable and row names.

Import Block of Mixed Data from Text File into Table or Cell Array

Import block formatted tabular data from a text file into a table or a cell array.

Import Numeric Data from Text Files into Matrix

In addition to importing numeric tabular data from a text file as a table using readtable, you can also import this data as a matrix into the MATLAB workspace.

Import Block of Numeric Data from Text File

Read arbitrary block-formatted numeric data from a text file into cell arrays.

Read and Analyze Large Tabular Text File

This example shows how to create a datastore for a large text file containing tabular data, and then read and process the data one block at a time or one file at a time.

Import Dates and Times from Text Files

Import formatted dates and times (such as '01/01/01' or '12:30:45') from column oriented tabular data.

Read Collection or Sequence of Text Files

Read and manage data from a collection of text files.

Define Import Options for Tables

If you want to control the import process beyond the options provided by the readtable function, such as defining how to handle missing data or errors, then create an import options object before importing the data.

Write Tabular Data to Text Files

Write Data to Text Files

Export tabular data contained in tables, cell arrays, or numeric arrays from the MATLAB workspace to text files.