MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export • Two spaces • A fixed-point value of twelve characters with eight decimal places fprintf converts the elements of array y in column order. The function uses the format string repeatedly until it converts all the array elements. Now use fscanf to read the exponential data file: fid = fopen('exptable.txt','r'); title = fgetl(fid); [table,count] = fscanf(fid,'%f %f',[2 11]); table = table'; status = fclose(fid); Thesecondlinereadsthefiletitle. Thethirdlinereadsthetableofvalues, two floating-point values on each line, until it reaches end of file. count returns the number of values matched. A function related to fprintf, sprintf, outputs its results to a string instead of a file or the screen. For example, root2 = sprintf('The square root of %f is %10.8e.\n',2,sqrt(2)); Closing a File When you finish reading or writing, use fclose to close the file. For example, this line closes the file associated with file identifier fid: status = fclose(fid); This line closes all open files: status = fclose('all'); Both forms return 0 if the file or files were successfully closed or -1 if the attempt was unsuccessful. MATLAB automatically closes all open files when you exit from MATLAB. It is still good practice, however, to close a file explicitly with fclose when you are finished using it. Not doing so can unnecessarily drain system resources. 6-114
Using Low-Level File I/O Functions Note Closing a file does not clear the file identifier variable fid. However, subsequent attempts to access a file through this file identifier variable will not work. 6-115
- 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 542 and 543: 6 Data Import and Export Note While
- 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 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
- Page 592 and 593: 7 Working with Scientific Data Form
- Page 594 and 595: 7 Working with Scientific Data Form
- Page 596 and 597: 7 Working with Scientific Data Form
- Page 598 and 599: 7 Working with Scientific Data Form
- Page 600 and 601: 7 Working with Scientific Data Form
Using Low-Level File I/O Functions<br />
Note Closing a file does not clear the file identifier variable fid. However,<br />
subsequent attempts to access a file through this file identifier variable will<br />
not work.<br />
6-115