gpt4 book ai didi

覆盖单元格数据时 MATLAB matfile 的大小会增加

转载 作者:行者123 更新时间:2023-12-02 08:23:16 28 4
gpt4 key购买 nike

由于数据量较大且自动保存频繁,我决定将保存方法从标准 save() 函数更改为使用 matfile 对象进行部分保存:

https://www.mathworks.com/help/matlab/ref/matfile.html

我进行此更改是因为即使对结构进行了微小的更改,使用 save() 也会覆盖所有内容,从而大大减慢程序速度。但是我注意到,每次调用 matfile 时,保存时间都会线性增加,经过一些调试后,我注意到这是由于文件大小每次都增加,即使数据被相同的数据覆盖。这是一个例子:

% Save MAT file with string variable and cell variable
stringvar = 'hello'
cellvar = {'world'}
save('test.mat', 'stringvar', 'cellvar', '-v7.3')
m = matfile('test.mat', 'Writable', true);
% Get number of bytes of MAT file
f = dir('test.mat'); f.bytes
% Output: 3928 - inital size
% Overwrite stringvar with same data.
m.stringvar = 'hello';
f = dir('test.mat'); f.bytes
% Output: 3928 - same as before
% Overwrite cellvar with same data.
m.cellvar = {'world'};
f = dir('test.mat'); f.bytes
% Output: 4544 - size increased

我不明白为什么数据相同时字节数会增加。它增加了非常明显的时间延迟,每次保存都会增加,因此它违背了部分保存的目的。知道这是怎么回事吗?对此的帮助将不胜感激!

最佳答案

这是由于元胞数组和更复杂的数据类型在 7.3 (HDF5) mat 文件中存储(和更新)的方式造成的。由于元胞数组包含混合数据类型,MATLAB 将元胞数组变量存储在根 (/) HDF5 group 中。作为一系列references指向包含 datasets/#refs# 组其中每个包含一个单元格的数据。

每当您尝试覆盖元胞数组值时,/#refs# HDF5 group被附加到新的 datasets代表元胞数组元素数据和 refrences/ group已更新以指向此新数据。旧的(现在未使用)datasets /#refs# 中的内容不会被删除。这是 HDF5 文件的设计行为,因为从文件中删除数据需要将删除区域后的所有文件内容移动以“缩小间隙”,这会导致(可能巨大的)性能损失**。

我们可以使用h5disp查看 MATLAB 正在创建的文件的内容来说明这一点。下面我将使用 h5disp 的缩写输出,以便更清晰:

stringvar = 'hello';
cellvar = {'world'};
save('test.mat', 'stringvar', 'cellvar', '-v7.3')

h5disp('test.mat')
% HDF5 test.mat
% Group '/'
% Dataset 'cellvar' <--- YOUR CELL ARRAY
% Size: 1x1 <--- HERE IS ITS SIZE
% Datatype: H5T_REFERENCE <--- THE ACTUAL DATA LIVES IN /#REFS#
% Attributes:
% 'MATLAB_class': 'cell'
% Dataset 'stringvar' <--- YOUR STRING
% Size: 1x5 <--- HAS 5 CHARACTERS
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% Group '/#refs#' <--- WHERE THE DATA FOR THE CELL ARRAY LIVES
% Attributes:
% 'H5PATH': '/#refs#'
% Dataset 'a'
% Size: 2
% Datatype: H5T_STD_U64LE (uint64)
% Attributes:
% 'MATLAB_empty': 1
% 'MATLAB_class': 'canonical empty'
% Dataset 'b' <--- THE CELL ARRAY DATA
% Size: 1x5 <--- CONTAINS A 5-CHAR STRING
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% 'H5PATH': '/#refs#/b'

%% Now we want to replace the string with a 6-character string
m.stringvar = 'hellos';
h5disp('test.mat')
% HDF5 test.mat
% Group '/'
% Dataset 'cellvar' <--- THIS REMAINS UNCHANGED
% Size: 1x1
% Datatype: H5T_REFERENCE
% Attributes:
% 'MATLAB_class': 'cell'
% Dataset 'stringvar'
% Size: 1x6 <--- JUST INCREASED THE LENGTH OF THIS TO 6
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% Group '/#refs#'
% Attributes:
% 'H5PATH': '/#refs#'
% Dataset 'a' <--- NONE OF THIS HAS CHANGED
% Size: 2
% Datatype: H5T_STD_U64LE (uint64)
% Attributes:
% 'MATLAB_empty': 1
% 'MATLAB_class': 'canonical empty'
% Dataset 'b'
% Size: 1x5
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% 'H5PATH': '/#refs#/b'

%% Now change the cell (and replace with a 6-character string)
m.cellvar = {'worlds'};
% HDF5 test.mat
% Group '/'
% Dataset 'cellvar' <--- HERE IS YOUR CELL ARRAY AGAIN
% Size: 1x1
% Datatype: H5T_REFERENCE <--- STILL A REFERENCE
% Attributes:
% 'MATLAB_class': 'cell'
% Dataset 'stringvar' <--- STRING VARIABLE UNCHANGED
% Size: 1x6
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% Group '/#refs#'
% Attributes:
% 'H5PATH': '/#refs#'
% Dataset 'a' <--- THE OLD DATA IS STILL HERE
% Size: 2
% Datatype: H5T_STD_U64LE (uint64)
% Attributes:
% 'MATLAB_empty': 1
% 'MATLAB_class': 'canonical empty'
% Dataset 'b' <--- THE OLD DATA IS STILL HERE
% Size: 1x5
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% 'H5PATH': '/#refs#/b'
% Dataset 'c' <--- THE NEW DATA IS ALSO HERE
% Size: 2
% Datatype: H5T_STD_U64LE (uint64)
% Attributes:
% 'MATLAB_empty': 1
% 'MATLAB_class': 'canonical empty'
% Dataset 'd' <--- THE NEW DATA IS ALSO HERE
% Size: 1x6 <--- NOW WITH 6 CHARACTERS
% Datatype: H5T_STD_U16LE (uint16)
% Attributes:
% 'MATLAB_class': 'char'
% 'MATLAB_int_decode': 2
% 'H5PATH': '/#refs#/d'

正是 #refs# 组的大小不断增加导致文件大小增加。由于#refs# 包含实际数据,因此每次保存文件时,您要替换的元胞数组元素中的所有数据都将被复制。

至于为什么 Mathworks 选择使用 HDF5 来处理 7.3 mat 文件,尽管这看似很大的限制,7.3 文件的动机似乎是为了帮助访问文件中的数据,而不是为了优化文件大小。

一种可能的解决方法是使用 7.0 格式,该格式是非 HDF5 格式,并且修改元胞数组变量时文件大小不会增加。 7.0 与 7.3 相比唯一真正的缺点是 can't modify just part of a variable in the 7.0 files 。另一个好处是,对于复杂数据,7.0 .mat 文件 are typically faster to read and write与 7.3 HDF5 文件相比。

% Helper function to tell us the size
printsize = @(filename)disp(getfield(dir(filename), 'bytes'));

stringvar = 'hello'
cellvar = {'world'}

% Save as 7.0 version
save('test.mat', 'stringvar', 'cellvar', '-v7')
printsize('test.mat')
% 256

m = matfile('test.mat', 'Writable', true);

m.stringvar = 'hello';
printsize('test.mat')
% 256

m.cellvar = {'world'};
printsize('test.mat')
% 256

如果您仍然想使用 7.3 文件,可能值得将元胞数组保存到临时变量,在函数中修改它,并且很少将其写回文件以防止不必要的写入。

tmp = m.cellvar;

% Make many modifications
tmp{1} = 'hello';
tmp{2} = 'world';
tmp{1} = 'Just kidding!';

% Write once after all changes have been made
m.cellvar = tmp;

** Normally you could use h5repack to reclaim the unused space in the file; however, MATLAB doesn't actually delete the data within /#refs# so h5repack has no effect. From what I gather, you'd have to delete the data yourself and then use h5repack to free up the unused space.

fid = H5F.open('test2.mat', 'H5F_ACC_RDWR', 'H5P_DEFAULT');

% I've hard-coded these names just as an example
H5L.delete(fid, '/#refs#/a', 'H5P_DEFAULT')
H5L.delete(fid, '/#refs#/b', 'H5P_DEFAULT')
H5F.close(fid);

system('h5repack test.mat test.repacked.mat');

关于覆盖单元格数据时 MATLAB matfile 的大小会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41526334/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com