2

我正在使用 Matlab 进行一些数据收集,并且我想在每次试验后保存数据(以防万一出现问题)。数据被组织为单元阵列的单元阵列,基本上采用以下格式

data{target}{trial} = zeros(1000,19)

但实际数据在收集结束时达到 >150 MB,因此在每次试验后保存所有内容变得异常缓慢。所以现在我正在考虑选择 matfile 方法(http://www.mathworks.de/de/help/matlab/ref/matfile.html),它只允许我保存部分数据。问题:这不支持单元阵列的单元,这意味着我无法更改/更新单个试验的数据;我将不得不重新保存整个目标的数据(100 次试验)。

所以,我的问题:

我可以使用另一种不同的方法来保存部分元胞数组以加快保存速度吗?

(或者)

有没有更好的方法来格式化我的数据以适应这个保存过程?

4

3 回答 3

1

一个不太优雅但可能有效的解决方案是trial用作变量名的一部分。也就是说,不要使用元胞数组 ( data{target}{trial}) 的元胞数组,而只是使用不同的元胞数组,例如data_1{target}, data_2{target},其中 1、2 是trial计数器的值。

你可以这样做eval:例如

trial = 1; % change this value in a for lopp
eval([ 'data_' num2str(trial) '{target} = zeros(1000,19);']); % fill data_1{target}

然后,您可以将每个试验的数据保存在不同的文件中。例如,这个

eval([ 'save temp_save_file_' num2str(trial) ' data_' num2str(trial)])

保存data_1在文件temp_save_file_1等中

于 2013-09-27T10:12:14.883 回答
0

In response to @Luis I will post an other way to deal with the situation.

It is indeed an option to save data in named variables or files, but to save a named variable in a named file seems too much.

If you only change the name of the file, you can save everything without using eval:

assuming you are dealing with trial 't':

filename = ['temp_save_file_' + num2str(t)];

If you really want, you can use print commands to write it as 001 for example.

Now you can simply use this:

save(filename, myData)

To use this, construct the filename again and so something like this:

totalData = {}; %Initialize your total data

And then read them as you wrote them (inside a loop):

load(filename)
totalData{t} = myData
于 2013-09-27T12:24:52.583 回答
0

更新:

实际上,它似乎可以索引到单元格数组,而不是在单元格数组之外。因此,如果您存储数据的方式略有不同,您似乎只能使用它matfile来更新其中的一部分。看这个例子:

x = cell(3,4);
save x;
matObj = matfile('x.mat','writable',true);
matObj.x(3,4) = {eye(10)};

请注意,这给了我一个版本警告,但它似乎有效。

希望这能解决问题。但是,请继续查看我的答案的下一部分,因为它可能会为您提供更多帮助。


对于计算,通常不需要在每次迭代后保存到磁盘。获得加速的一种简单方法(以增加一点风险为代价)是仅在每 n 次试验后保存。

像这样的例子:

maxTrial = 99;
saveEvery = 10;

for trial = 1:maxTrial
   myFun; %Do your calculations here
   if trial == maxTrial || mod(trial, saveEvery) == 0
      save %Put your save command here
   end
end

如果您的数据始终处于(或处于)特定大小,您还可以选择将数据存储在矩阵而不是元胞数组中,然后您可以使用索引来仅保存文件的一部分。

于 2013-09-27T09:57:36.690 回答