如何在 matlab 中将每次运行的输出值保存在同一文件中

发布于 2024-10-06 06:05:50 字数 210 浏览 0 评论 0原文

我在每次运行时得到两个不同的(数字和字符)输出值,我想将这些值保存在文件中,以便在另一个进程中使用它们。我将它们保存在数组结构中,然后将它们保存在(mat 文件)中,如下所示:

Sim(i).No Sim(i).Nam

save('Sim', 'Sim")

我想在每次运行时将这些值保存在同一个文件中......问题是在下一次运行中我只得到上次运行的值。

I get two different (numeric and character )output values at each run, I want to save these value in a file in order to use them in another process. I saved them in a struct of array then I saved them in (mat file) as follow:

Sim(i).No
Sim(i).Nam

save('Sim', 'Sim")

I want to saved these value at each run in the same file ... the problem is in the next run I get just the values of the last run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-10-13 06:05:50

您可以使用 SAVE< 的 '-append' 选项/a> 将数据添加到已保存的文件中。但是,您需要小心地使用不同的名称保存每次运行,否则您将覆盖保存文件。

因此,你可以这样做:

for i=1:nRuns

%# create Sim

%# make variable name containing 'i'
simName = ['Sim_',num2str(i)];
saveStruct.(simName) = Sim;
%# save field 'Sim_#', where # is the value of i, to a file 'Sim'
%# in the first iteration, we cannot use append, since the file doesn't exist yet.
if i==1
save('Sim','-struct','saveStruct',simName)
else
save('Sim','-struct','saveStruct',simName,'-append')
end

%# to save memory, re-initialize saveStruct
saveStruct = struct;

end %# loop

You can use the '-append' option of SAVE to add data to a saved file. However, you need to be careful to save each run with a different name, otherwise you'll just be overwriting the save file.

Thus, you could do something like this:

for i=1:nRuns

%# create Sim

%# make variable name containing 'i'
simName = ['Sim_',num2str(i)];
saveStruct.(simName) = Sim;
%# save field 'Sim_#', where # is the value of i, to a file 'Sim'
%# in the first iteration, we cannot use append, since the file doesn't exist yet.
if i==1
save('Sim','-struct','saveStruct',simName)
else
save('Sim','-struct','saveStruct',simName,'-append')
end

%# to save memory, re-initialize saveStruct
saveStruct = struct;

end %# loop
怪我太投入 2024-10-13 06:05:50

您正在尝试增量地将结构数组保存到磁盘,因此每次都需要加载、追加和重新保存数组(对于非常大的数据,不建议这样做)

% load existing Sim.mat
if exist('Sim.mat','file')
   load('Sim')
end
% check Sim was indeed loaded and if so extend it
if exist('Sim','var') && ~isempty(Sim)
   Sim(end+1).No = No;
   Sim(end+1).Nam = Nam;
else 
   % create a new Sim (presumably the first time)
   Sim(1).No = No;
   Sim(1).Nam = Nam;
end
% save the exented Sim
save Sim Sim

:上面假设您在代码前面定义了 NoNam
如果您在循环中运行它,更好的方法是将 Sim 保留在内存中并在循环结束时保存整个结构体数组。

You are trying to save-to-disk a struct array incrementally, so you need to load, appended, and re-save the array each time (something that is not recommended for very large data):

% load existing Sim.mat
if exist('Sim.mat','file')
   load('Sim')
end
% check Sim was indeed loaded and if so extend it
if exist('Sim','var') && ~isempty(Sim)
   Sim(end+1).No = No;
   Sim(end+1).Nam = Nam;
else 
   % create a new Sim (presumably the first time)
   Sim(1).No = No;
   Sim(1).Nam = Nam;
end
% save the exented Sim
save Sim Sim

The above assumes you have No and Nam defined earlier in the code.
If you were running this in a loop, a better approach would be to just keep Sim in memory and save the entire struct array at the end of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文