readtable

  • readtable(filename) returns a table based on the delimited text file specified in filename. First line contains variable names
  • readtable(filename,'HasNames',op)
    • op = true (default): takes first row as variable names
    • op = false: adds default variable names
  • readtable(filename,'HeaderLines',n)
    • n = 0 (default): takes first line as variable names
    • n > 0: skips first n rows
    • n = -1: adds default variable names
  • readtable(filename,'Delimiter',delim) will use delim (one letter string) as delimiter
  • readtable(str,'isstring',true) will convert the string str into a table

Examples

>> T1 = readtable('readtable.example.txt')#

  Name Grade     DOB
  John     B  Jun-12
  Marc     A  Feb-12
   Sam     B  Oct-11
  Toni     F  Aug-12
  
>> T2 = readtable('readtable.example.txt','headerlines', 3)#

   Sam    B   Oct-11
  Toni    F   Aug-12
  
>> T3 = readtable('readtable.example.txt','headerlines', 4, 'hasNames', false)#

  Col1 Col2    Col3
  Toni    F  Aug-12

>> T4 = readtable('readtable.example.txt','headerlines', -1)#

  Col1 Col2     Col3
  John    B   Jun-12
  Marc    A   Feb-12
   Sam    B   Oct-11
  Toni    F   Aug-12
>> T5 = readtable('readtable.example.dat','delimiter','|')#

  Name Grade     DOB
  John     B  Jun-12
  Marc     A  Feb-12
   Sam     B  Oct-11
  Toni     F  Aug-12
>> str = ['Name,Grade,DOB
John,B,Jun-12
Marc,A,Feb-12
Sam,B,Oct-11
Toni,F,Aug-12'] 

% creating a csv string

>> readtable(str,'isstring',true)

% converting str into a table

[ans:4x3 table]

Resources

See also

table | writetable