Main Content

Read Text File Data Using Import Tool

Import data from a text file by selecting data interactively. You also can repeat this import operation on multiple text files by using the generate code feature of the import tool.

Select Data Interactively

This example shows how to import data from a text file with column headers and numeric data using the Import Tool. The file in the example, grades.txt, contains this data:

   John    Ann     Mark    Rob
   88.4    91.5    89.2    77.3
   83.2    88.0    67.8    91.0
   77.8    76.3            92.5
   92.1    96.4    81.2    84.6

To create the file, copy and paste the data using any text editor.

On the Home tab, in the Variable section, click Import Data . Alternatively, right-click the name of the file in the Current Folder browser and select Import Data. The Import Tool opens.

grades.txt is a sample file used to portray the Import Tool.

The Import Tool recognizes that grades.txt is a fixed width file. In the Imported Data section, select how you want the data to be imported. The following table indicates how data is imported depending on the option you select.

Option SelectedHow Data is Imported
TableImport selected data as a table.
Column vectorsImport each column of the selected data as an individual m-by-1 vector.
Numeric MatrixImport selected data as an m-by-n numeric array.
String ArrayImport selected data as a string array that contains text.
Cell ArrayImport selected data as a cell array that can contain multiple data types, such as numeric data and text.

Under Delimiter Options, you can specify whether the Import Tool should use a period or a comma as the decimal separator for numeric values.

The Delimiter Options are located within the Delimiters section of the Import tab.

Double-click a variable name to rename it.

The variable name is located as a column header.

You also can use the Variable Names Row box in the Selection section to select the row in the text file that you want the Import Tool to use for variable names.

The Import Tool highlights unimportable cells. Unimportable cells are cells that contain data that cannot be imported in the format specified for that column. In this example, the cell at row 3, column C, is considered unimportable because a blank cell is not numeric. Highlight colors correspond to proposed rules to make the data fit into a numeric array. You can add, remove, reorder, or edit rules, such as changing the replacement value from NaN to another value.

All rules apply to the imported data only and do not change the data in the file. Any time you are importing into a matrix or into numeric column vectors and the range includes non-numeric data, then you must specify the rules.

To see how your data is imported, place the cursor over individual cells.

For example, information on how a value was replaced might be shown

When you click the Import Selection button , the Import Tool creates variables in your workspace.

For more information on interacting with the Import Tool, watch this video.

Import Data from Multiple Text Files

To perform the same import operation on multiple files, use the code generation feature of the Import Tool. If you import a file one time and generate code from the Import Tool, you can use this code to make it easier to repeat the operation. The Import Tool generates a program script that you can edit and run to import the files, or a function that you can call for each file.

Suppose you have a set of text files in the current folder. The files are named myfile01.txt through myfile25.txt, and you want to import the data from each file, starting from the second row. Generate code to import the entire set of files as follows:

  1. Open one of the files in the Import Tool.

  2. Click Import Selection , and then select Generate Function. The Import Tool generates code similar to the following excerpt, and opens the code in the Editor.

    function data = importfile(filename,startRow,endRow)
    %IMPORTFILE Import numeric data from a text file as a matrix.
    ...
  3. Save the function.

  4. In a separate program file or at the command line, create a for loop to import data from each text file into a cell array named myData:

    numFiles = 25;
    startRow = 2;
    endRow = inf;
    myData = cell(1,numFiles);
    
    for fileNum = 1:numFiles
        fileName = sprintf('myfile%02d.txt',fileNum);
        myData{fileNum} = importfile(fileName,startRow,endRow);
    end

Each cell in myData contains an array of data from the corresponding text file. For example, myData{1} contains the data from the first file, myfile01.txt.

See Also

| | | | |

Related Topics