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 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

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

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

Saved successfully!

Ooh no, something went wrong!