MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 492 and 493: 6 Data Import and Export Note To su
- Page 494 and 495: 6 Data Import and Export d = m.Data
- Page 496 and 497: 6 Data Import and Export ans = 166x
- Page 498 and 499: 6 Data Import and Export If you hav
- Page 500 and 501: 6 Data Import and Export m.Data(60)
- Page 502 and 503: 6 Data Import and Export m = memmap
- Page 504 and 505: 6 Data Import and Export d = m.Data
- Page 506 and 507: 6 Data Import and Export Data: 1658
- Page 508 and 509: 6 Data Import and Export One MATLAB
- Page 510 and 511: 6 Data Import and Export disp('ANSW
- Page 512 and 513: 6 Data Import and Export Terminatin
- Page 514 and 515: 6 Data Import and Export character.
- Page 516 and 517: 6 Data Import and Export Table6-2 A
- Page 518 and 519: 6 Data Import and Export A = dlmrea
- Page 520 and 521: 6 Data Import and Export number of
- Page 522 and 523: 6 Data Import and Export Exporting
- Page 524 and 525: 6 Data Import and Export A = [ 1 2
- Page 526 and 527: 6 Data Import and Export diary off
- Page 528 and 529: 6 Data Import and Export Importing
- Page 530 and 531: 6 Data Import and Export Working wi
- Page 532 and 533: 6 Data Import and Export Exporting
- Page 534 and 535: 6 Data Import and Export 3 Close th
- Page 536 and 537: 6 Data Import and Export type = Mic
- Page 538 and 539: 6 Data Import and Export ndata = xl
- Page 540 and 541: 6 Data Import and Export 41 42 43 4
- Page 544 and 545: 6 Data Import and Export Use these
- Page 546 and 547: 6 Data Import and Export Writing Bi
- Page 548 and 549: 6 Data Import and Export The act of
- Page 550 and 551: 6 Data Import and Export Conversion
- Page 552 and 553: 6 Data Import and Export • Two sp
- Page 554 and 555: 6 Data Import and Export Exchanging
- Page 556 and 557: 6 Data Import and Export urlread('h
- Page 558 and 559: 6 Data Import and Export Once you h
- Page 560 and 561: 6 Data Import and Export For exampl
- Page 562 and 563: 6 Data Import and Export 6-124
- Page 564 and 565: 7 Working with Scientific Data Form
- Page 566 and 567: 7 Working with Scientific Data Form
- Page 568 and 569: 7 Working with Scientific Data Form
- Page 570 and 571: 7 Working with Scientific Data Form
- Page 572 and 573: 7 Working with Scientific Data Form
- Page 574 and 575: 7 Working with Scientific Data Form
- Page 576 and 577: 7 Working with Scientific Data Form
- Page 578 and 579: 7 Working with Scientific Data Form
- Page 580 and 581: 7 Working with Scientific Data Form
- Page 582 and 583: 7 Working with Scientific Data Form
- Page 584 and 585: 7 Working with Scientific Data Form
- Page 586 and 587: 7 Working with Scientific Data Form
- Page 588 and 589: 7 Working with Scientific Data Form
- Page 590 and 591: 7 Working with Scientific Data Form
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