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 />

Use these functions to create temporary files. Some systems delete temporary<br />

files every time you reboot the system. On other systems, designating a file as<br />

temporary can mean only that the file is not backed up.<br />

The tempdir function returns the name of the directory or folder that has<br />

been designated to hold temporary files on your system. For example, issuing<br />

tempdir on a UNIX system returns the /tmp directory.<br />

<strong>MATLAB</strong> also provides a tempname function that returns afilenameinthe<br />

temporary directory. The returned filename is a suitable destination for<br />

temporary data. For example, if you need to store some data in a temporary<br />

file, then you might issue the following command first:<br />

fid = fopen(tempname, 'w');<br />

Note The filename that tempname generates is not guaranteed to be unique;<br />

however, it is likely to be so.<br />

Reading Binary Data<br />

The fread function reads all or part of a binary file (as specified by a file<br />

identifier) and stores it in a matrix. In its simplest form, it reads an entire<br />

file and interprets each byte of input as the next element of the matrix. For<br />

example, the following code reads the data from a file named nickel.dat<br />

into matrix A:<br />

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

A = fread(fid);<br />

To echo the data to the screen after reading it, use char to display the contents<br />

of A as characters, transposing the data so it is displayed horizontally:<br />

disp(char(A'))<br />

The char function causes <strong>MATLAB</strong> to interpret the contents of A as characters<br />

instead of as numbers. Transposing A displays it in its more natural<br />

horizontal format.<br />

6-106

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

Saved successfully!

Ooh no, something went wrong!