Skip to main content

9.3) Reading Text Files


Before we look at reading text files, it is useful to see how to load data using the copy and paste function. This is perhaps the easiest method, yet the most cumbersome:

  1. In a text file or spreadsheet, use the Copy command (copy to clipboard).
  2. Then in Matlab, run the command:
  3. [matlab]
    >> data=clipboard('pastespecial');
    [/matlab]

  4. Matlab's import wizard will appear. It allows you to check that the data is OK and to change the import settings if necessary.
  5. This creates a structural array. To convert this into a standard array, we can use statements such as:
  6. [matlab]
    >> a=data.A_pastespecial;
    [/matlab]

So how can we load text files directly?

  • If your data is stored in an ascii file with no header(column titles), then the data can be loaded directly into Matlab using the "load" command:
  • [matlab]
    >> a=load('filename.txt');
    [/matlab]

The load function is quite rudimentary: it cannot handle headers or data when there are missing entries. Next we will look at more robust methods.