Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Import Mixed Text and Numeric Data from Text Files

Read File with Column Names

This example shows how to use the readtable function to import a text file with column and row headings.

Create a tab-delimited text file named grades.txt that contains the following (copy and paste into a text editor):

Class Grades for Spring Term
	Grade1	Grade2	Grade3
John	85	90	95
Ann	90	92	98
Martin	100	95	97
Rob	77	86	93

This file contains one header line followed by a row of column names, Grade1, Grade2, and Grade3.

Call readtable to read the file. Use the name-value pair argument, HeaderLines, to specify one header line to ignore. Use the name-value pair argument, Delimiter, to specify a tab delimiter.

T = readtable('grades.txt','HeaderLines',1,'Delimiter','\t')
T = 

      Var1      Grade1    Grade2    Grade3
    ________    ______    ______    ______

    'John'       85       90        95    
    'Ann'        90       92        98    
    'Martin'    100       95        97    
    'Rob'        77       86        93    

readtable returns a 4-by-4 table. By default, readtable reads variable names from the first row of the file following the header lines. Because the first column of data in the file does not have text in the first row, readtable assigns that variable the name, Var1.

Read the file again, this time reading the first column of data as row names.

T = readtable('grades.txt','HeaderLines',1,...
	'Delimiter','\t','ReadRowNames',true)
T = 

              Grade1    Grade2    Grade3
              ______    ______    ______

    John       85       90        95    
    Ann        90       92        98    
    Martin    100       95        97    
    Rob        77       86        93    

readtable returns a 4-by-3 table with row names and variable names.

Read File Without Column Names

This example shows how to use the readtable function to import a text file with no column headings.

Create a text file named results.dat that contains the following (copy and paste into a text editor):

Sally 09/12/2005 12.34 45 Yes
Larry 10/12/2005 34.56 54 Yes
Tommy 11/12/2005 67.89 23 No

This file contains data without any column names.

Call readtable to read the file. By default, readtable treats the first row in a file as variable names. Use the name-value pair argument, ReadVariableNames, to tell readtable not to treat the first row in the file as variable names.

T = readtable('results.dat','Delimiter',' ','ReadVariableNames',false)
T = 

     Var1          Var2        Var3     Var4    Var5 
    _______    ____________    _____    ____    _____

    'Sally'    '09/12/2005'    12.34    45      'Yes'
    'Larry'    '10/12/2005'    34.56    54      'Yes'
    'Tommy'    '11/12/2005'    67.89    23      'No' 

readtable returns a 3-by-5 table and assigns default names to the table variables.

View the values in the table variable, Var4, using dot notation.

T.Var3
ans =

    45
    54
    23

See Also

More About

Was this topic helpful?