MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

6 Data Import and Export Note While the MATLAB file I/O commands are modeled on the C language I/O routines, in some ways their behavior is different. For example, the fread function is vectorized; that is, it continues reading until it encounters a text string or the end of file. These sections, and the MATLAB reference pages for these functions, highlight any differences in behavior. Opening Files Before reading or writing a text or binary file, you must open it with the fopen command. fid = fopen('filename','permission') Specifying the Permission String The permission string specifies the kind of access to the file you require. Possible permission strings include • r for reading only • w for writing only • a for appending only • r+ for both reading and writing Note SystemssuchasMicrosoftWindowsthat distinguish between text and binary files might require additional characters in the permission string, such as 'rb' to open a binary file for reading. Using the Returned File Identifier (fid) If successful, fopen returns a nonnegative integer, called a file identifier (fid). You pass this value as an argument to the other I/O functions to access the open file. For example, this fopen statement opens the data file named penny.dat for reading: fid = fopen('penny.dat','r') 6-104

Using Low-Level File I/O Functions If fopen fails, for example if you try to open a file that does not exist, fopen • Assigns -1 to the file identifier. • Assigns an error message to an optional second output argument. Note that the error messages are system dependent and are not provided for all errors on all systems. The function ferror can also provide information about errors. Test the file identifier each time you open a file in your code. For example, this code loops until a readable filename is entered: fid=0; while fid < 1 filename=input('Open file: ', 's'); [fid,message] = fopen(filename, 'r'); if fid == -1 disp(message) end end When you run this code, if you specify a file that doesn’t exist, such as nofile.mat, attheOpen file: prompt, the results are Open file: nofile.mat Sorry. No help in figuring out the problem . . . If you specify a file that does exist, such as goodfile.mat, the code example returns the file identifier, fid, and exits the loop. Open file: goodfile.mat Opening Temporary Files and Directories The tempdir and tempname functions assist in locating temporary data on your system. Function tempdir tempname Purpose Get temporary directory name. Get temporary filename. 6-105

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

If fopen fails, for example if you try to open a file that does not exist, fopen<br />

• Assigns -1 to the file identifier.<br />

• Assigns an error message to an optional second output argument. Note<br />

that the error messages are system dependent and are not provided for all<br />

errors on all systems. The function ferror can also provide information<br />

about errors.<br />

Test the file identifier each time you open a file in your code. For example,<br />

this code loops until a readable filename is entered:<br />

fid=0;<br />

while fid < 1<br />

filename=input('Open file: ', 's');<br />

[fid,message] = fopen(filename, 'r');<br />

if fid == -1<br />

disp(message)<br />

end<br />

end<br />

When you run this code, if you specify a file that doesn’t exist, such as<br />

nofile.mat, attheOpen file: prompt, the results are<br />

Open file: nofile.mat<br />

Sorry. No help in figuring out the problem . . .<br />

If you specify a file that does exist, such as goodfile.mat, the code example<br />

returns the file identifier, fid, and exits the loop.<br />

Open file: goodfile.mat<br />

Opening Temporary Files and Directories<br />

The tempdir and tempname functions assist in locating temporary data on<br />

your system.<br />

Function<br />

tempdir<br />

tempname<br />

Purpose<br />

Get temporary directory name.<br />

Get temporary filename.<br />

6-105

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

Saved successfully!

Ooh no, something went wrong!