Programming and Data Types    


This section covers the following topics:

File I/O Function Overview

For a good overview of MATLAB file I/O functions, use the online Functions by Category reference. In the Help Browser Contents, click on MATLAB -> Functions -- By Category, and then click on 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 -- By Category" 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 by typing 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 Files in the MATLAB Development Environment documentation

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:


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.

For more information: See Obtaining User Input in the MATLAB "Programming and Data Types" documentation

  Files and Filenames Managing Memory