Skip to Main Content Skip to Search
Product Documentation

Recommended Methods for Importing Data

Tools that Import Multiple File Formats

The easiest way to import data into MATLAB from a disk file or the system clipboard is to use the Import Wizard, a graphical user interface. The Import Wizard helps you find a file and define the variables to use in the MATLAB workspace.

To import data from a file, start the Import Wizard, using one of these methods:

To import data from the clipboard, start the Import Wizard, using one of these methods:

To import without invoking a graphical user interface, the easiest option is to use the importdata function.

The Import Wizard reads all supported file formats, except netCDF, HDF5, Motion JPEG 2000, and platform-specific video. The importdata function reads all formats supported by the Import Wizard, except HDF4.

For more information, see Tips for Using the Import Wizard.

Importing Specific File Formats

MATLAB includes functions tailored to import these specific file formats:

Consider using format-specific functions instead of the Import Wizard or importdata when:

For a complete list of the format-specific functions, see Supported File Formats.

Importing Data in Other Formats

If the Import Wizard, importdata, and format-specific functions cannot read your data, use low-level I/O functions such as fscanf or fread. Low-level functions allow the most control over reading from a file, but require detailed knowledge of the structure of your data. For more information, see:

Alternatively, MATLAB toolboxes perform specialized import operations. For example, use Database Toolbox™ software for importing data from relational databases. Refer to the documentation on specific toolboxes to see the available import features.

Finding Files

To find a specific file on the MATLAB search path, use the which function. If the file is not in the current folder, include the full or partial path with the file name in calls to import functions.

For example, to locate and load myfile.mat:

fullname = which('myfile.mat');
load(fullname);

For more information, see:

Processing a Sequence of Files

To import or export multiple files, create a control loop to process one file at a time. When constructing the loop:

For example, to read files named file1.txt through file20.txt with importdata:

numfiles = 20;
mydata = cell(1, numfiles);

for k = 1:numfiles
  myfilename = sprintf('file%d.txt', k);
  mydata{k} = importdata(myfilename);
end

To read all files that match *.jpg with imread:

jpegFiles = dir('*.jpg'); 
numfiles = length(jpegFiles);
mydata = cell(1, numfiles);

for k = 1:numfiles 
  mydata{k} = imread(jpegFiles(k).name); 
end

Tips for Using the Import Wizard

Start the Import Wizard by selecting File > Import Data or calling uiimport.

The Import Wizard provides the following options for reading text files, images, audio, or video data:

Viewing the Contents of a File

For text files, the Import Wizard automatically displays a preview of the data in the file.

To view images or video, or to listen to audio, click the Back button on the first window that the Import Wizard displays.

The right pane of the new window includes a preview tab. Click the button in the preview tab to show an image or to play audio or video.

Specifying Variables

The Import Wizard generates default variable names based on the format and content of your data. You can change the variables in any of the following ways:

The default variable name for data imported from the system clipboard is A_pastespecial.

If the Import Wizard detects a single variable in a file, the default variable name is the file name. Otherwise, the Import Wizard uses default variable names that correspond to the output fields of the importdata function. For more information on the output fields, see the importdata function reference page.

Renaming or Deselecting Variables.  To override the default variable name, select the name and type a new one.

To avoid importing a particular variable, clear the check box in the Import column.

Importing to a Structure Array.  To import data into fields of a structure array rather than as individual variables, start the Import Wizard by calling uiimport with an output argument. For example, the demo file durer.mat contains three variables: X, caption, and map. If you issue the command

durerStruct = uiimport('durer.mat')

and click the Finish button, the Import Wizard returns a scalar structure with three fields:

durerStruct = 
          X: [648x509 double]
        map: [128x3 double]
    caption: [2x28 char]

To access a particular field, use dot notation. For example, view the caption field:

disp(durerStruct.caption)

MATLAB returns:

Albrecht Durer's Melancolia.
Can you find the matrix?  

For more information, see Structures in the MATLAB Programming Fundamentals documentation.

Generating Reusable MATLAB Code

To create a function that reads similar files without restarting the Import Wizard, select the Generate MATLAB code check box. When you click Finish to complete the initial import operation, MATLAB opens an Editor window that contains an unsaved function. The default function name is importfile.m or importfileN.m, where N is an integer.

The function in the generated code includes the following features:

However, the generated code has the following limitations:

MATLAB does not automatically save the function. To save the file, select File > Save. For best results, use the function name with a .m extension for the file name.

Example — Generating Code to Import a Text File.  Consider the file grades.txt discussed in Import Data with Headers Interactively. The file contains the following data:

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

Use the Import Wizard to create column vectors, select the Generate MATLAB code check box, and click Finish. MATLAB generates code similar to the following excerpt, and opens the code in the Editor.

function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
%  Imports data from the specified file
%  FILETOREAD1:  file to read
...

Save the function. Create a file new_grades.txt that contains the following data:

   Michael  Juan    Nita    Steve   Lynn
   76.3     89.0    93.1    72.4    81.7
   81.9     93.4    90.5    81.8    76.7
   80.3     97.8   100.0    89.2    79.6

Import the data using the generated function:

importfile1('new_grades.txt')

The workspace includes the following variables:

  Name         Size            Bytes  Class     Attributes

  Juan         3x1                24  double              
  Lynn         3x1                24  double              
  Michael      3x1                24  double              
  Nita         3x1                24  double              
  Steve        3x1                24  double              
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS