| MATLAB Environment | Search  Help Desk |
Data Import/Export
These topics provide information on importing and exporting data with MATLAB:Importing Data into MATLAB
You can introduce data from other programs into MATLAB using several methods. The best method for importing data depends on the amount and format of the data.| Method |
When to Use | |
| Enter data as an explicit list of elements |
If you have a small amount of data, it is easy to type the data explicitly using brackets ([ ]). | |
| Create data in an M-file |
Use a text editor to create an M-file that enters the data as an explicit list of elements. Use your text editor to change the data and to fix mistakes. Then just rerun the M-file to re-enter the data. | |
Load data from an ASCII data file (load) |
An ASCII data file stores the data in ASCII form, with each row having the same number of values, and terminating with new lines (carriage returns), with spaces separating the numbers. Edit ASCII data files using a normal text editor. Read ASCII data files directly into MATLAB using the load function. This creates a variable whose name is the same as the filename. Use dlmread if you need to specify alternate value delimiters. | |
Read data using fopen, fread, and MATLAB's file I/O functions |
Use these commands to load data files from other applications that have their own established file formats. These functions are discussed in detail in Chapter 15 of Using MATLAB. | |
| Use a specialized file reader function for application-specific formats |
|
Read ASCII data file. |
|
Read string and numeric data from a file into MATLAB variables using conversion specifiers. | |
|
Read spreadsheet (WK1) file. |
|
|
Read image from graphics file. |
|
|
Read Sun (.au) sound file. |
|
|
Read Microsoft WAVE (.wav) sound file. |
|
| Develop a MEX-file to read the data |
Use this method if C or Fortran routines are already available for reading data files from other applications. See the MATLAB Application Program Interface Guide for more information. | |
| Develop a Fortran or C translation program |
Develop a program to translate your data into MAT-file format and then read the MAT-file into MATLAB with the load command. See the MATLAB Application Program Interface Guide for more information. | |
Exporting Data from MATLAB
There are several methods for getting MATLAB data to other applications:| Method |
When to Use | |
Use the diary command |
For small arrays, use the diary command to create a diary file and display the variables, echoing them into this file. Use your text editor to edit the diary file, removing unwanted text. | |
| Save the data in ASCII form |
Use the save command with the -ascii option. See Loading and Saving the Workspace for details on save. You can also use dlmwrite if you need to specify alternate value delimiters. | |
| Write the data in a special format |
Use fwrite and the other low-level I/O functions to write data files in the formats required by other applications. These functions are discussed in detail in Chapter 15 of Using MATLAB. | |
| Use a specialized file write function for application-specific formats |
|
Write ASCII data file. |
|
Write spreadsheet (WK1) file. |
|
|
Write image to graphics file. |
|
|
Write Sun (.au) sound file. |
|
|
Write Microsoft WAVE (.wav) sound file. |
|
| Develop a MEX-file to write the data |
Use this method if C or Fortran routines are already available for writing data files in the form needed by other applications. See the MATLAB Application Program Interface Guide for more information. | |
| Write out the data as a MAT-file |
Use the save command, and then write a program in Fortran or C to translate the MAT-file into the desired format. See the MATLAB Application Program Interface Guide for more information. | |
Exchanging Data Files Between Platforms
It is sometimes necessary to work with MATLAB implementations on different computer systems, or to transmit MATLAB applications to users on other systems. MATLAB applications consist of M-files, containing functions and scripts, and MAT-files, containing binary data. Both types of files can be transported directly between different computers:The diary Command
Usediary to create a verbatim copy of your MATLAB session in a disk file (excluding graphics). View and edit the resulting text file using any word processor. To stop recording the session, use diary off.