Skip to main content

9.4) Reading Text Files (cont'd)


The textread and textscan functions allow you to import mixed classes of text-file data including headers and/or missing values. You can also specify the delimitation (command delimited, white space), the format of the data and how to identify comment lines within the data (to ignore).

  • "textread" returns the data as separate objects.
  • "textscan" returns all the data in a cell array.

Both are typically used in combination with a description of the text data format. However, textscan is now recommended for all text import requirements.

In the following example, we use textscan and specify data_format as three numbers (%n) followed by a string (%s).

[matlab]
>> fid=fopen('data.txt', 'r');
>> data_format='%n %n %n %s';
>> a=textscan(fid, data_format)
[/matlab]

For full descriptions, see the help pages for textread and textscan.