- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于数据量较大且自动保存频繁,我决定将保存方法从标准 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#
soh5repack
has no effect. From what I gather, you'd have to delete the data yourself and then useh5repack
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/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!