tdfread

Read tab-delimited file

Description

tdfread opens the Select File to Open dialog box for interactive selection of a data file, and reads the data from the file you select. tdfread can read data from tab-delimited text files with .txt, .dat, or .csv file extensions.

Select a file that has variable names in the first row and values separated by tabs in the remaining rows. tdfread creates a variable in the workspace for each column of the file, and names each variable according to its first row value.

  • If a column contains only numeric data in all rows except the first, then tdfread creates a double variable.

  • Otherwise, tdfread creates a char variable.

After importing all values, tdfread displays information about the imported variables, such as their size, bytes, and class.

tdfread(filename) creates variables from the data in filename, which is either the name of a file in the current folder or the complete path name of a file.

example

tdfread(filename,delimiter) indicates that the character specified by delimiter separates values in the file.

s = tdfread(___) returns a structure s in which each field contains a variable. Specify any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Display the contents of the sat2.dat file. Note that the first row of the file contains the variable names.

type sat2.dat
Test,Gender,Score
Verbal,Male,470
Verbal,Female,530
Quantitative,Male,520
Quantitative,Female,480

In the workspace, create the variables Gender, Score, and Test from the columns of the file. Because commas separate the values in the file, specify ',' as the delimiter.

tdfread('sat2.dat',',')
  Name        Size            Bytes  Class     Attributes

  Gender      4x6                48  char                
  Score       4x1                32  double              
  Test        4x12               96  char                

Input Arguments

collapse all

Name of the file to read, specified as a character vector or string scalar.

Depending on the location of the file, filename has one of these forms.

Location of FileForm
Current folder or folder on the MATLAB® path

Specify the name of the file in filename.

Example: 'myTextFile.txt'

Folder that is not the current folder or a folder on the MATLAB path

Specify the full or relative path name in filename.

Example: 'C:\myFolder\myTextFile.txt'

Example: 'sat2.dat'

Data Types: char | string

Delimiter character, specified as one of the values in this table.

ValueDescription

'|'

'bar'

Vertical bar

','

'comma'

Comma

';'

'semi'

Semicolon

' '

'space'

Space

'\t'

'tab'

Tab

Example: ','

Data Types: char | string

Alternative Functionality

Consider using the readtable, readmatrix, or readcell MATLAB functions to import data. These functions provide more flexible data importing options than tdfread.

Introduced before R2006a