Main Content

Map File to Memory

创建一个简单的内存图

假设您要为名称的文件创建存储映射records.dat, 使用memmapfile功能。

创建一个名称的示例文件records.dat,包含5000个值。

rng('default')mydata = rand([5000,1]);fileid = fopen('records.dat',,,,'W');fwrite(fileid,mydata,'双倍的');fclose(fileID);

Next, create the memory map. Use theFormat名称值对参数指定值是类型的值双倍的。使用可写name-value pair argument to allow write access to the mapped region.

m= memmapfile('records.dat',,,,...'格式',,,,'双倍的',,,,...“可写”,,,,true)
m= Filename: 'd:\matlab\records.dat' Writable: true Offset: 0 Format: 'double' Repeat: Inf Data: 5000x1 double array

MATLAB®创建一个memmapfileobject,m。这Formatproperty indicates that read and write operations to the mapped region treat the data in the file as a sequence of double-precision numbers. TheData属性包含文件中的5000个值,records.dat。You can change the value of any of the properties, except forData,创建内存图后,m

For example, change the starting position of the memory map,m。Begin the mapped region 1024 bytes from the start of the file by changing the value of the抵消财产。

m。抵消= 1024
m= Filename: 'd:\matlab\records.dat' Writable: true Offset: 1024 Format: 'double' Repeat: Inf Data: 4872x1 double array

每当您更改内存映射属性的值时,MATLAB都会将文件重建为存储器。这Data属性现在仅包含4872个值。

指定映射数据的格式

默认情况下,MATLAB将映射文件中的所有数据视为未签名的8位整数的序列。但是,您的数据可能是不同的数据类型。当您致电memmapfile功能,使用Formatname-value pair argument to indicate another data type. The value ofFormat可以是标识整个映射区域中使用的单个类的字符向量,也可以是指定多个类的单元格数组。

假设您映射一个长度为12千元的文件。从该文件中读取的数据可以视为6,000个16位(2字节)整数的序列,也可以将其视为1,500个8字节双精度浮点数,仅举几例可能性。您还可以将这些数据读成不同类型的组合:例如,4,000个8位(1字节)整数,然后是1,000个64位(8字节)整数。您可以通过设置MATLAB如何解释映射数据Format调用内存图的属性memmapfile功能。

MATLAB数组以列订单存储在磁盘上。数组元素的序列是第1列第1列;第1列,第2行;第1列,最后一行;第2列,第1行等。通过内存图读取或写作时,您可能需要转置或重新排列数组元素的顺序。

映射多个数据类型和数组

如果您要映射的区域包括不同的数据类型或数组形状的段,则可以为每个段指定单个格式。指定值Format名称值对参数作为一个n-by-3单元阵列,其中nis the number of segments. Each row in the cell array corresponds to a segment. The first cell in the row identifies the data type to apply to the mapped segment. The second cell contains the array dimensions to apply to the segment. The third cell contains the field name for referencing that segment. For a memory map,m,使用以下语法:

m = memmapfile(文件name,...'格式',,,,{...datatype1,dimensions1,fieldName1;...datatype2,dimensions2,fieldName2;...: : :...datatypen,dimensionsn,fieldamen})

假设您的文件长40,000个字节。以下代码映射从2048th Byte开始的数据。这Format值是一个3 x-3个单元格数组,将文件数据映射到三个不同类:int16,,,,UINT32,,,,and单身的

m= memmapfile('records.dat',,,,...'Offset',2048年,...'格式',,,,{...'int16'[2 2]'模型';...'uint32'[1 1]'序列号';...'单身的'[1 3]'花费'});

在这种情况下,memmapfile地图int16data as a 2-by-2 matrix that you can access using the field name,模型。这UINT32data is a scalar value accessed using the field name,serialno。这单身的data is a 1-by-3 matrix namedexpenses。Each of these fields belongs to the 800-by-1 structure array,M.Data

This figure shows the mapping of the example file.

下图更加紧密地显示了数组元素的排序。特别是,它说明MATLAB数组以列序列存储在磁盘上。映射文件中数组元素的序列是第1行,第1列;第2行,第1列;第1行,第2列;和第2行,第2列。

If the data in your file is not stored in this order, you might need to transpose or rearrange the order of array elements when reading or writing via a memory map.

选择要映射的文件

您可以更改文件名构造后的任何时间memmapfile目的。如果以下方式,您可能需要这样做

  • You want to use the samememmapfileobject on more than one file.

  • 你保存你的memmapfile在垫子文件中对抗,然后在映射文件已移至其他位置的环境中,然后将其加载到MATLAB中。这要求您修改文件名代表新位置。

Update the path in the文件名使用点表示法的存储映射属性。例如,要指定新路径,f:\ testfiles \ records.datfor a memory map,m,,,,type:

m.FileName ='f:\ testfiles \ records.dat'

也可以看看

Related Topics