MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export Accessing Files with Memory-Mapping Memory-mapping is a mechanism that maps a portion of a file, or an entire file, on disk to a range of addresses within an application’s address space. The application can then access files on disk in the same way it accesses dynamic memory. This makes file reads and writes faster in comparison with using functions such as fread and fwrite. Another advantage of using memory-mapping in MATLAB is that it enables you to access file data using standard MATLAB indexing operations. Once you have mapped a file to memory, you can read the contents of that file using the same type of MATLAB statements used to read variables from the MATLAB workspace. The contents of the mapped file appear as if they were an array in the currently active workspace. You simply index into this array to read or write the desired data from the file. This section covers • “Overview of Memory-Mapping in MATLAB” on page 6-34 • “The memmapfile Class” on page 6-38 • “Constructing a memmapfile Object” on page 6-40 • “Reading a Mapped File” on page 6-54 • “Writing to a Mapped File” on page 6-59 • “Methods of the memmapfile Class” on page 6-67 • “Deleting a Memory Map” on page 6-69 • “Memory-Mapping Demo” on page 6-69 Overview of Memory-Mapping in MATLAB This section describes the benefits and limitations of memory-mapping in MATLAB. The last part of this section gives details on which types of applications derive the greatest advantage from using memory-mapping: • “Benefits of Memory-Mapping” on page 6-35 • “Limitations of Memory-Mapping in MATLAB” on page 6-36 • “When to Use Memory-Mapping” on page 6-37 6-34
Accessing Files with Memory-Mapping Benefits of Memory-Mapping The principal benefits of memory-mapping are efficiency, faster file access, the ability to share memory between applications, and more efficient coding. Faster File Access. Accessing files via memory map is faster than using I/O functions such as fread and fwrite. Data is read and written using the virtual memory capabilities that are built in to the operating system rather than having to allocate, copy into, and then deallocate data buffers owned by the process. MATLAB does not access data from the disk when the map is first constructed. It only reads or writes the file on disk when a specified part of the memory map is accessed, and then it only reads that specific part. This provides faster random access to the mapped data. Efficiency. Mapping a file into memory allows access to data in the file as if that data had been read into an array in the application’s address space. Initially, MATLAB only allocates address space for the array; it does not actually read data from the file until you access the mapped region. As a result, memory-mapped files provide a mechanism by which applications can access data segments in an extremely large file without having to read the entire file into memory first. Efficient Coding Style. Memory-mapping eliminates the need for explicit calls to the fread and fwrite functions. In MATLAB, if x is a memory-mapped variable, and y is the data to be written to a file, then writing to the file is as simple as x.Data = y; 6-35
- Page 422 and 423: 5 Types of Functions You can now us
- Page 424 and 425: 5 Types of Functions Using a Subfun
- Page 426 and 427: 5 Types of Functions count = 125 22
- Page 428 and 429: 5 Types of Functions shape. The mak
- Page 430 and 431: 5 Types of Functions % FILTFCN. Eac
- Page 432 and 433: 5 Types of Functions 0.6667 filt2(1
- Page 434 and 435: 5 Types of Functions unless you dec
- Page 436 and 437: 5 Types of Functions Accessing Help
- Page 438 and 439: 5 Types of Functions 5-38
- Page 440 and 441: 6 Data Import and Export Working wi
- 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 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 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
6 Data Import and Export<br />
Accessing Files with Memory-Mapping<br />
Memory-mapping is a mechanism that maps a portion of a file, or an entire<br />
file, on disk to a range of addresses within an application’s address space. The<br />
application can then access files on disk in the same way it accesses dynamic<br />
memory. This makes file reads and writes faster in comparison with using<br />
functions such as fread and fwrite.<br />
Another advantage of using memory-mapping in <strong>MATLAB</strong> is that it enables<br />
you to access file data using standard <strong>MATLAB</strong> indexing operations. Once<br />
you have mapped a file to memory, you can read the contents of that file<br />
using the same type of <strong>MATLAB</strong> statements used to read variables from the<br />
<strong>MATLAB</strong> workspace. The contents of the mapped file appear as if they were<br />
an array in the currently active workspace. You simply index into this array<br />
to read or write the desired data from the file.<br />
This section covers<br />
• “Overview of Memory-Mapping in <strong>MATLAB</strong>” on page 6-34<br />
• “The memmapfile Class” on page 6-38<br />
• “Constructing a memmapfile Object” on page 6-40<br />
• “Reading a Mapped File” on page 6-54<br />
• “Writing to a Mapped File” on page 6-59<br />
• “Methods of the memmapfile Class” on page 6-67<br />
• “Deleting a Memory Map” on page 6-69<br />
• “Memory-Mapping Demo” on page 6-69<br />
Overview of Memory-Mapping in <strong>MATLAB</strong><br />
This section describes the benefits and limitations of memory-mapping<br />
in <strong>MATLAB</strong>. The last part of this section gives details on which types of<br />
applications derive the greatest advantage from using memory-mapping:<br />
• “Benefits of Memory-Mapping” on page 6-35<br />
• “Limitations of Memory-Mapping in <strong>MATLAB</strong>” on page 6-36<br />
• “When to Use Memory-Mapping” on page 6-37<br />
6-34