Select Spreadsheet Data Using Import Tool

Select Data Interactively

This example shows how to import data from a spreadsheet into the workspace with the Import Tool. The worksheet in this example includes three columns of data labeled Station, Temp, and Date:

Station   Temp    Date
12          98    9/22/2013
13        x       10/23/2013
14          97    12/1/2013

On the Home tab, in the Variable section, click Import Data . Alternatively, in the Current Folder browser, double-click the name of a file with an extension of .xls, .xlsx, .xlsb, or .xlsm. The Import Tool opens.

Select the data you want to import. 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
Column vectorsImport each column of the selected data as an individual m-by-1 vector.
MatrixImport selected data as an m-by-n numeric array.
Cell ArrayImport selected data as a cell array that can contain multiple data types, such as numeric data and text.
TableImport selected data as a table.

For example, the data in the following figure corresponds to data for three column vectors. You can edit the variable name within the tab, and you can select noncontiguous sections of data for the same variable.

If you choose to import as a matrix or numeric column vectors, the tool highlights any nonnumeric data in the worksheet. Each highlight color corresponds to a proposed rule to make the data fit into a numeric array. For example, you can replace nonnumeric values with NaN. You can see how your data will be imported when you place the cursor over individual cells.

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.

Any cells that contain #Error? correspond to formula errors in your spreadsheet file, such as division by zero. The Import Tool regards these cells as nonnumeric.

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 videovideo.

Import Data from Multiple Spreadsheets

If you plan to perform the same import operation on multiple files, you can generate code from the Import Tool to make it easier to repeat the operation. On all platforms, the Import Tool can generate a program script that you can edit and run to import the files. On Microsoft® Windows® systems with Excel® software, the Import Tool can generate a function that you can call for each file.

For example, suppose you have a set of spreadsheets in the current folder named myfile01.xlsx through myfile25.xlsx, and you want to import the same range of data, A2:G100, from the first worksheet in each file. Generate code to import the entire set of files as follows:

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

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

    function data = importfile(workbookFile, sheetName, range)
    %IMPORTFILE    Import numeric data from a spreadsheet
    ...
  3. Save the function.

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

    numFiles = 25;
    range = 'A2:G100';
    sheet = 1;
    myData = cell(1,numFiles);
    
    for fileNum = 1:numFiles
        fileName = sprintf('myfile%02d.xlsx',fileNum);
        myData{fileNum} = importfile(fileName,sheet,range);
    end

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

Was this topic helpful?