该containers.Map
对象与任何其他对象一样是 Matlab 对象。在引擎盖下,这些被实现为带有一些附加访问控制和函数映射的 Matlab 结构。
您可以强制 Matlab 使用该struct
命令向您显示原始结构。这会引发警告,因为通常不建议这样做。但是,类的结构视图显示了完整的内容,并准确地反映在whos
调用中。
一些示例代码如下:
%Initialize map and add some content
res_Map = containers.Map;
for ix = 1:1000
res_Map(sprintf('%05d',ix)) = ix;
end
%Look at the memory used by the map
disp('Raw who: always 112 Bytes for Map')
whos('res_Map')
%Force the map into a structure, and look at the contained memory
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')
%Add additional contents and check again.
for ix = 1001:2000
res_Map(sprintf('%05d',ix)) = ix;
end
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')
上述脚本的结果(删除警告消息后)如下所示:
Raw who: always 112 Bytes for Map
Name Size Bytes Class Attributes
res_Map 1000x1 112 containers.Map
Look at the size of the map contents, reflect true size
Name Size Bytes Class Attributes
mapContents 1x1 243621 struct
res_Map 1000x1 112 containers.Map
Look at the size of the map contents, reflect true size
Name Size Bytes Class Attributes
mapContents 1x1 485621 struct
res_Map 2000x1 112 containers.Map