| MATLAB® | ![]() |
| On this page… |
|---|
For more information and examples on importing and exporting data, see Technical Note 1602:
http://www.mathworks.com/support/tech-notes/1600/1602.html
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.
Note For unsupported high-level function data formats, you can use the MATLAB low-level file I/O functions if you know how the binary data is formatted in the file. For more information, see Using Low-Level File I/O Functions. |
MATLAB has built-in capabilities to import and export the following types of data files:
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.
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.
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.
MATLAB provides functions to enable you to interact with the following types of audio and audio/video files:
NeXT/Sun SPARC®station sound
Microsoft WAVE sound
Audio/Video Interleaved (AVI)
Sound devices compatible with Microsoft Windows
Audio player and recorder objects
Linear audio signals
You can use MATLAB to import and export data to the following types of spreadsheets:
Microsoft® Excel® spreadsheets
Lotus 123 spreadsheets
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.
From your MATLAB session, you can
Send e-mail
Download from the Internet
Compress (zip) and decompress (unzip) files
Connect to an FTP server to perform remote file operations
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.
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.
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);
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.
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.
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:
| Function | Description |
|---|---|
| cd | Switch your current working directory to another directory |
| clipboard | Copy and paste strings to and from the system clipboard |
| copyfile | Copy a file or directory to another location |
| delete | Delete the specified files |
| dir | List the files that reside in the specified directory |
| edit | Create a new M-file or edit an existing one |
| exist | Check the existence of a file or directory |
| fileattrib | Set or get attributes of a file or directory |
| filebrowser | Start the Current Directory Browser |
| fileparts | Show the components of a file name and its place on the path |
| fullfile | Build a full file name from its components |
| ls | List the contents of a specific directory |
| mkdir | Create a new directory |
| movefile | Move a file or directory to a new location |
| open | Open files based on extension |
| pwd | Identify the directory you are currently working in |
| recycle | Set an option to move deleted files to recycle folder |
| rmdir | Delete a specific directory |
| what | List the MATLAB files in a specific directory |
| which | Locate functions and files |
![]() | Data Import and Export | Supported File Formats | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |