23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Low-Level File I/O Functions<br />

Using Low-Level File I/O Functions<br />

<strong>MATLAB</strong> includes a set of low-level file I/O functions that are based on the I/O<br />

functions of the ANSI Standard C Library. If you know C, you are probably<br />

familiar with these routines. This section covers<br />

• “Opening Files” on page 6-104<br />

• “Reading Binary Data” on page 6-106<br />

• “Writing Binary Data” on page 6-108<br />

• “Controlling Position in a File” on page 6-108<br />

• “Reading Strings Line by Line from Text Files” on page 6-110<br />

• “Reading Formatted ASCII Data” on page 6-111<br />

• “Writing Formatted Text Files” on page 6-113<br />

• “Closing a File” on page 6-114<br />

The<strong>MATLAB</strong>fileI/OfunctionsusethesameprogrammingmodelastheC<br />

language routines. To read or write data, you perform these steps:<br />

1 Open the file, using fopen. fopen returns a file identifier that you use with<br />

all the other low-level file I/O routines.<br />

2 Operate on the file.<br />

a Read binary data, using fread.<br />

b Write binary data, using fwrite.<br />

c<br />

Read text strings from a file line-by-line, using fgets or fgetl.<br />

d Read formatted ASCII data, using fscanf.<br />

e<br />

Write formatted ASCII data, using fprintf.<br />

3 Close the file, using fclose.<br />

This section also describes how these functions affect the current position<br />

inthefilewhere read or write operations happen and how you can change<br />

the position in thefile.<br />

6-103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!