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

Loading Binary Data<br />

MAT-files are double-precision binary <strong>MATLAB</strong> format files created by the<br />

save function and readable by the load function. They can be created on one<br />

machine and later read by <strong>MATLAB</strong> on another machine with a different<br />

floating-point format, retaining as much accuracy and range as the different<br />

formats allow. They can also be manipulated by other programs, external<br />

to <strong>MATLAB</strong>.<br />

MAT-filescancontaindatainanuncompressedoracompressedform,orboth.<br />

<strong>MATLAB</strong> knows which variables in the file have been compressed by looking<br />

at a tag that it attaches to each variable during the save operation. When<br />

loading data from a MAT-file into the workspace, <strong>MATLAB</strong> automatically<br />

handles the decompression of the appropriate data.<br />

The External Interface libraries contain C- and Fortran-callable routines to<br />

read and write MAT-files from external programs.<br />

Loading ASCII Data<br />

ASCII files must be organized as a rectangular table of numbers, with each<br />

number in a row separated by a blank, comma, or tab character, and with<br />

an equal number of elements in each row. <strong>MATLAB</strong> generates an error if<br />

the number of values differs between any two rows. ASCII files can contain<br />

<strong>MATLAB</strong> comments (lines that begin with %).<br />

<strong>MATLAB</strong> returns all the data in the file as a single two-dimensional array<br />

of type double. The number of rows in the array is equal to the number of<br />

lines in the file, and the number of columns is equal to the number of values<br />

on a line.<br />

In the workspace, <strong>MATLAB</strong> assigns the array to a variable named after the<br />

file being loaded (minus any file extension). For example, the command<br />

load mydata.dat<br />

reads all of the data from mydata.dat into the <strong>MATLAB</strong> workspace as a single<br />

array, and assigns it to a variable called mydata. Innamingthevariable,load<br />

precedes any leading underscores or digits in filename with an X and replaces<br />

any other nonalphabetic characters with underscores.<br />

6-32

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

Saved successfully!

Ooh no, something went wrong!