23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6 Data Import and Export<br />

Note While the <strong>MATLAB</strong> file I/O commands are modeled on the C language<br />

I/O routines, in some ways their behavior is different. For example, the fread<br />

function is vectorized; that is, it continues reading until it encounters a text<br />

string or the end of file. These sections, and the <strong>MATLAB</strong> reference pages for<br />

these functions, highlight any differences in behavior.<br />

Opening Files<br />

Before reading or writing a text or binary file, you must open it with the<br />

fopen command.<br />

fid = fopen('filename','permission')<br />

Specifying the Permission String<br />

The permission string specifies the kind of access to the file you require.<br />

Possible permission strings include<br />

• r for reading only<br />

• w for writing only<br />

• a for appending only<br />

• r+ for both reading and writing<br />

Note SystemssuchasMicrosoftWindowsthat distinguish between text and<br />

binary files might require additional characters in the permission string, such<br />

as 'rb' to open a binary file for reading.<br />

Using the Returned File Identifier (fid)<br />

If successful, fopen returns a nonnegative integer, called a file identifier<br />

(fid). You pass this value as an argument to the other I/O functions to access<br />

the open file. For example, this fopen statement opens the data file named<br />

penny.dat for reading:<br />

fid = fopen('penny.dat','r')<br />

6-104

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

Saved successfully!

Ooh no, something went wrong!