Overview

For more information and examples on importing and exporting data, see Technical Note 1602:

http://www.mathworks.com/support/tech-notes/1600/1602.html

Supported File Types

The MATLAB software provides many ways to load data from disk files or the clipboard into the workspace, a process called importing data. Also there are many ways to save workspace variables to a disk file, a process called exporting data. Your choice of which import or export mechanism to use depends mainly on the format of the data being transferred: text, binary, or a standard format such as JPEG.

MATLAB has built-in capabilities to import and export the following types of data files:

Binary Data from a MATLAB Session

Using the MATLAB save and load functions, you can store all or part of the data in your MATLAB workspaces to disk, and then read that data back into MATLAB at a later time.

Text Data

In text format, the data values are American Standard Code for Information Interchange (ASCII) codes that represent alphabetic and numeric characters. You can view ASCII text data in a text editor. For more information about working with text data in MATLAB, see these sections:

They also describe how to import and export to XML documents.

Graphics Files

MATLAB imports and exports images from many standard graphics file formats, including the Tagged Image File Format (TIFF), Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPEG), and Portable Network Graphics (PNG) formats.

Audio and Audio/Video Data

MATLAB provides functions to enable you to interact with the following types of audio and audio/video files:

Spreadsheets

You can use MATLAB to import and export data to the following types of spreadsheets:

Data from the System Clipboard

Using the Import Wizard or the clipboard function, you can temporarily hold string data on your system's clipboard, and then paste it back into MATLAB.

Information from the Internet

From your MATLAB session, you can

Other MATLAB I/O Capabilities

Using the Import Wizard

The Import Wizard is a graphical user interface that simplified the process of locating and loading various types of data files into MATLAB. You do not need to know the format of the data to use this tool. You simply specify the file that contains the data and the Import Wizard processes the file contents automatically. See the section on Using the Import Wizard.

Mapping Files to Memory

Memory—mapping enables you to read and write data in a file as if were stored in the computer's dynamic memory. The contents of the mapped file appear as if they were an array in the currently active workspace. You simply index into this array to read or write the desired data from the file. See the section on Accessing Files with Memory-Mapping.

Reading Files with Large Data Sets

An efficient way to read files with large data sets is to read the file in segments and process the data as you go. This method requires significantly less memory than if you were to try reading in the entire file at once. Using the textscan function, you can read a specified amount of data from a file, and maintain a pointer to the location in the file where your last read operation ended and your next read is to begin.

This example opens a large data file and reads the file a segment at a time in a for loop. The code calls textscan to read a particular pattern of data (as specified by format) 10,000 times for each segment. Following each read, the subfunction process_data processes the data collected in cell array segarray:

format = '%s %n %s %8.2f %8.2f %8.2f %8.2f %u8';
file_id = fopen('largefile.dat', 'r');

for k = 1:segcount
   segarray = textscan(file_id, format, 10000);
   process_data(segarray);
end

fclose(file_id);

Low-Level File I/O

MATLAB also supports C-style, low-level I/O functions that you can use with any data format. For more information, see Using Low-Level File I/O Functions.

Importing Data with Toolboxes

In addition to MATLAB import functions, you can perform specialized import features using toolboxes. For example, use Database Toolbox™ software for importing data from relational databases. Refer to the documentation on the specific toolbox to see what import features are offered.

Functions Used in File Management

The following functions are available in MATLAB to help you to create, manage, and locate the files and directories you work with. For more information on these and other file management functions, see Managing Files and Working with the Current Directory in the MATLAB Desktop Tools and Development Environment documentation:

FunctionDescription
cdSwitch your current working directory to another directory
clipboardCopy and paste strings to and from the system clipboard
copyfileCopy a file or directory to another location
deleteDelete the specified files
dirList the files that reside in the specified directory
editCreate a new M-file or edit an existing one
existCheck the existence of a file or directory
fileattribSet or get attributes of a file or directory
filebrowserStart the Current Directory Browser
filepartsShow the components of a file name and its place on the path
fullfileBuild a full file name from its components
lsList the contents of a specific directory
mkdirCreate a new directory
movefileMove a file or directory to a new location
openOpen files based on extension
pwdIdentify the directory you are currently working in
recycleSet an option to move deleted files to recycle folder
rmdirDelete a specific directory
whatList the MATLAB files in a specific directory
whichLocate functions and files

  


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