Programming Previous page   Next Page

Input/Output

This section covers the following topics:

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

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

File I/O Function Overview

For a good overview of MATLAB file I/O functions, use the online "Functions -- Categorical List" reference. In the Help browser Contents, select MATLAB -> Functions -- Categorical List, and then click File I/O.

Common I/O Functions

The most commonly used, high-level, file I/O functions in MATLAB are save and load. For help on these, type doc save or doc load.

Functions for I/O to text files with delimited values are textread, dlmread, dlmwrite. Functions for I/O to text files with comma-separated values are csvread, csvwrite.

For more information: See Text Files in the MATLAB "Functions -- Categorical List" reference documentation.

Readable File Formats

Type doc fileformats to see a list of file formats that MATLAB can read, along with the associated MATLAB functions.

Using the Import Wizard

A quick method of importing text or binary data from a file (e.g., Excel files) is to use the MATLAB Import Wizard. Open the Import Wizard with the command, uiimport filename or by selecting File -> Import Data at the Command Window.

Specify or browse for the file containing the data you want to import and you will see a preview of what the file contains. Select the data you want and click Finish.

For more information: See Using the Import Wizard with Text Data and Using the Import Wizard with Binary Data.

Loading Mixed Format Data

To load data that is in mixed formats, use textread instead of load. The textread function lets you specify the format of each piece of data.

If the first line of file mydata.dat is

Read the first line of the file as a free format file using the % format:

returns

Reading Files with Different Formats

Attempting to read data from a file that was generated on a different platform may result in an error because the binary formats of the platforms may differ. Using the fopen function, you can specify a machine format when you open the file to avoid these errors.

Reading ASCII Data into a Cell Array

A common technique used to read an ASCII data file into a cell array is

For more information: See the textread and cellstr function reference pages.

Interactive Input into Your Program

Your program can accept interactive input from users during execution. Use the input function to prompt the user for input, and then read in a response. When executed, input causes the program to display your prompt, pause while a response is entered, and then resume when the Enter key is pressed.


Previous page  Files and Filenames Starting MATLAB Next page

© 1994-2005 The MathWorks, Inc.