Programming Previous page   Next Page

Writing to a Mapped File

Writing to a mapped file is done with standard MATLAB subscripted assignment commands. To write to a particular location in the file mapped to memmapfile object m, assign the value to the m.data structure array index and field that map to that location.

Call the memmapfile constructor to create the object:

If you are going to modify the mapped file, be sure that you have write permission, and that you set the writable property of the memmapfile object to true (logical 1):

(You do not have to set writable as a separate command, as done here. You can include a writable parameter-value argument in the call to the memmapfile constructor.)

Read from the 5-by-8 matrix x at m.data(2):

Update all values in that matrix using a standard MATLAB assignment statement:

Verify the results:

Dimensions of the Data Field

The dimensions of a memmapfile object's data field are set at the time you construct the object and cannot be changed. This differs from other MATLAB arrays that have dimensions you can modify using subscripted assignment.

For example, you can add a new column to the field of a MATLAB structure:

But not to a similar field of a structure that represents data mapped from a file. The following assignment to m.data(60).y does not expand the size of y, but instead generates an error:

If you need to modify the dimensions of data that you have mapped to a memmapfile object, you must either modify the format or repeat properties for the object, or reconstruct the object.

Examples.   Two examples of statements that attempt to modify the dimensions of a mapped data field are shown here. These statements result in an error.

The first example attempts to diminish the size of the array by removing a row from the mapped array m.data.

The second example attempts to expand the size of a 50-row mapped array x by adding another row to it:

Writing Matrices to a Mapped File

The syntax to use when writing to mapped memory can depend on what format was used when you mapped memory to the file.

When Memory Is Mapped in Nonstructure Format.   When you map a file as a sequence of a single data type (e.g., a sequence of uint16), you can use the following syntax to write matrix X to the file:

This statement is valid only if all of the following conditions are true:

This example maps a file as a sequence of 16-bit unsigned integers, and then uses the syntax shown above to write an entire matrix to the file. Start by constructing the map:

Create another matrix X of the same size:

Write matrix X to the mapped file:

When Memory Is Mapped in Scalar Structure Format.   When you map a file as a sequence of a single data type (e.g., a sequence of uint16), you can use the following syntax to write matrix X to the file:

This statement is valid only if all of the following conditions are true:

This example maps a file as a 300-by-8 matrix of type uint16 followed by a 200-by-5 matrix of type double, and then uses the syntax shown above to write a matrix to the file.

When Memory Is Mapped in Nonscalar Structure Format.   When you map a file as a repeating sequence of multiple data types, you can use the following syntax to write matrix X to the file, providing that k is a scalar index:

To do this, the following conditions must be true:

This example maps a file as a matrix of type uint16 followed by a matrix of type double that repeat 20 times, and then uses the syntax shown above to write a matrix to the file.

You can write to specific elements of field x as shown here:

Selecting Appropriate Data Types

All of the usual MATLAB indexing and data type rules apply when assigning values to data via a memory map. The data type that you assign to must be big enough to hold the value being assigned. For example,

saturates the x variable because x is defined as an 8-bit integer:

Working with Copies of the Mapped Data

In the following code, the data in variable block2 is a copy of the file data mapped by m.data(2). Because it is a copy, modifying array data in block2 does not modify the data contained in the file:

Verify that the data in the mapped file has not changed even though the copy of m.data(2).x has been written with zeros:

Invalid Syntax for Writing to Mapped Memory

Although you can expand the dimensions of a typical MATLAB array by assigning outside its current dimensions, this does not apply to the data property of a memmapfile object. The following operation is invalid if m.data has only 100 elements:

If you need to expand the size of the mapped data region, first extend the map by updating the format or repeat property of the memmapfile object to reflect the new structure of the data in the mapped file.

Example -- Writing Multiple Data Types

Create a memory map, mapping the data as 100 doubles, followed by 400 int16s, followed by 1000 doubles. The mapped segments are named obj.data.x, obj.data.y, and obj.data.z:

Change the data on disk:

Plot the results:


Previous page  Reading a Mapped File Methods of the memmapfile Class Next page

© 1994-2005 The MathWorks, Inc.