MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export Note To successfully modify the file you are mapping to, you must have write permission for that file. If you do not have write permission, you can still set the Writable property to true, but attempting to write to the file generates an error. You can change the value of the Writable property at any time. To make the memory map to records.dat read only, type m.Writable = false; Property names, like Writable, arenotcasesensitive. Reading a Mapped File The most commonly used property of the memmapfile classistheData property. It is through this property of the memory-map object that MATLAB provides all read and write access to the contents of the mapped file. TheactualmappingofafiletotheMATLABaddressspacedoesnottake place when you construct a memmapfile object. A memory map, based on the information currently stored in the mapped object, is generated the first time you reference or modify the Data property for that object. Once you have mapped a file to memory, you can read the contents of that file using the same MATLAB statements used to read variables from the MATLAB workspace. By accessing the Data property of the memory map object, the contents of the mapped fileappearasiftheywereanarrayin the currently active workspace. You simply index into this array to read the desired data from the file. This section covers the following topics: • “Improving Performance” on page 6-55 • “Example 1 — Reading a Single Data Type” on page 6-55 • “Example 2 — Formatting File Data as a Matrix” on page 6-56 • “Example 3 — Reading Multiple Data Types” on page 6-57 6-54
Accessing Files with Memory-Mapping • “Example 4 — Modifying Map Parameters” on page 6-58 Improving Performance MATLAB accesses data in structures more efficiently than it does data contained in objects. The main reason is that structures do not require the extra overhead of a subsref routine. Instead of reading directly from the memmapfile object, as shown here for k = 1 : N y(k) = m.Data(k); end you will get better performance when you assign the Data field to a variable and then read or write the mapped file through this variable, as shown in this second example: dataRef = m.Data; for k = 1 : N y(k) = dataRef(k); end Example 1 — Reading a Single Data Type This example maps a file of 100 double-precision floating-point numbers to memory. The map begins 1024 bytes from the start of the file, and ends 800 bytes (8 bytes per double times a Repeat value of 100) from that point. If you haven’t done so already, generate a test data file for use in the following examples by executing the gendatafile function defined under “Constructing a memmapfile Object” on page 6-40: gendatafile('records.dat', 5000); Now, construct the memmapfile object m, and show the format of its Data property: m = memmapfile('records.dat', 'Format', 'double', ... 'Offset', 1024, 'Repeat', 100); 6-55
- Page 442 and 443: 6 Data Import and Export • “Dat
- Page 444 and 445: 6 Data Import and Export file that
- Page 446 and 447: 6 Data Import and Export Function r
- Page 448 and 449: 6 Data Import and Export Importing
- Page 450 and 451: 6 Data Import and Export • rowhea
- Page 452 and 453: 6 Data Import and Export File to im
- Page 454 and 455: 6 Data Import and Export Example of
- Page 456 and 457: 6 Data Import and Export was select
- Page 458 and 459: 6 Data Import and Export Import fro
- Page 460 and 461: 6 Data Import and Export File Forma
- Page 462 and 463: 6 Data Import and Export or save ju
- Page 464 and 465: 6 Data Import and Export Data Compr
- Page 466 and 467: 6 Data Import and Export Preference
- Page 468 and 469: 6 Data Import and Export Saving fro
- Page 470 and 471: 6 Data Import and Export Loading Bi
- Page 472 and 473: 6 Data Import and Export Accessing
- Page 474 and 475: 6 Data Import and Export Sharing Me
- Page 476 and 477: 6 Data Import and Export • For sm
- Page 478 and 479: 6 Data Import and Export Note Prope
- Page 480 and 481: 6 Data Import and Export Writable:
- Page 482 and 483: 6 Data Import and Export Ifthefilet
- Page 484 and 485: 6 Data Import and Export For exampl
- Page 486 and 487: 6 Data Import and Export m = memmap
- Page 488 and 489: 6 Data Import and Export Mapping of
- Page 490 and 491: 6 Data Import and Export Repeating
- 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
6 Data Import and Export<br />
Note To successfully modify the file you are mapping to, you must have write<br />
permission for that file. If you do not have write permission, you can still set<br />
the Writable property to true, but attempting to write to the file generates<br />
an error.<br />
You can change the value of the Writable property at any time. To make the<br />
memory map to records.dat read only, type<br />
m.Writable = false;<br />
Property names, like Writable, arenotcasesensitive.<br />
Reading a Mapped File<br />
The most commonly used property of the memmapfile classistheData<br />
property. It is through this property of the memory-map object that <strong>MATLAB</strong><br />
provides all read and write access to the contents of the mapped file.<br />
Theactualmappingofafiletothe<strong>MATLAB</strong>addressspacedoesnottake<br />
place when you construct a memmapfile object. A memory map, based on the<br />
information currently stored in the mapped object, is generated the first time<br />
you reference or modify the Data property for that object.<br />
Once you have mapped a file to memory, you can read the contents of that<br />
file using the same <strong>MATLAB</strong> statements used to read variables from the<br />
<strong>MATLAB</strong> workspace. By accessing the Data property of the memory map<br />
object, the contents of the mapped fileappearasiftheywereanarrayin<br />
the currently active workspace. You simply index into this array to read the<br />
desired data from the file.<br />
This section covers the following topics:<br />
• “Improving Performance” on page 6-55<br />
• “Example 1 — Reading a Single Data Type” on page 6-55<br />
• “Example 2 — Formatting File Data as a Matrix” on page 6-56<br />
• “Example 3 — Reading Multiple Data Types” on page 6-57<br />
6-54