MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!