This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Select Text File Data Using 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 this example, grades.txt, contains the following data (to create the file, use any text editor, and copy and paste):

   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

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.

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.

Double-click on a variable name to rename it.

You also can use the Variable Names Row box in the Selection section to select the row in the text file that the Import Tool uses 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. You must specify rules any time the range includes nonnumeric data and you are importing into a matrix or numeric column vectors.

You can see how your data will be imported when you place the cursor over individual cells.

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

This example shows how to perform the same import operation on multiple files using the Import Tool. You can generate code from the Import Tool, making 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 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);

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.

Related Topics

Was this topic helpful?