48
Constructing a memmapfile Object
objname = memmapfile(filename, 'Format', formatspec)
The
formatspec
argument can either be a character string that identifies a
single class used throughout the mapped region, or a cell array that specifies
more than one class.
For example, say that you map a file that is 12 kilobytes in length. Data read
from this file could be treated as a sequence of 6,000 16-bit (2-byte) integers,
or as 1,500 8-byte double-precision floating-point numbers, to name just a
couple of possibilities. Or you could read this data in as a combination of
different types: for example, as 4,000 8-bit (1-byte) integers followed by 1,000
64-bit (8-byte) integers. You determine how MATLAB will interpret the
mapped data by setting the
Format
property of the
memmapfile
object when
you call its constructor.
MATLAB arrays are stored on disk in column-major order. (The sequence
of array elements is column 1, row 1; column 1, row 2; column 1, last row;
column 2, row 1, and so on.) You might need to transpose or rearrange the
order of array elements when reading or writing via a memory map.
For a list of data types supported for the
Format
property, see “Supported
Data Types for the Format Property” on page 4-20.
For more information on format options see these sections:
• “Mapping a Single Data Type” on page 4-15
• “Formatting the Mapped Data to an Array” on page 4-16
• “Mapping Multiple Data Types and Arrays” on page 4-17
Mapping a Single Data Type
If the file region being mapped contains data of only one type, specify the
Format
value as a character string identifying that type:
objname = memmapfile(filename, 'Format', datatype)
The following command constructs a
memmapfile
object for the entire file
records.dat
,and sets the
Format
property for that object to
uint64
. Any
read or write operations made via the memory map will read and write the
file contents as a sequence of unsigned 64-bit integers:
4-15