加载多个.mat文件进行处理 [英] Load multiple .mat files for processing

查看:511
本文介绍了加载多个.mat文件进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MatLab中,我(在广泛的代码运行之后)将多个.mat文件输出到.mat文件。每个.mat文件的实际matlab名称称为 results 但我使用 save 命令将它们写入不同的文件。这些文件的一小部分如下所示:

In MatLab I have (after extensive code running) multiple .mat files outputted to .mat files. The actual matlab name of each .mat file is called results but I've used the save command to write them to different files. A small subset of the files looks like this:

results_test1_1.mat
results_test1_2.mat
results_test1_3.mat
results_test1_4.mat

results_test2_1.mat
results_test2_2.mat
results_test2_3.mat
results_test2_4.mat

现在我想比较每个测试的结果,这意味着我必须加载所有四个.mat文件并将它们组合在一个图形中。读取一个文件并制作最终图形是没有问题的。但由于所有文件都具有相同的matlab名称结果,因此迭代加载它们不是一个选项(至少不是我所知道的那个),因为最后只有文件4保留,因为它重写了以前的那些。

Now I want to compare the results for each test, which means I have to load in all four .mat files and combine them in a graph. Reading in one file and making the eventual graph is no problem. But since all files have the same matlab name results, iteratively loading them is not an option (at least, not one that I know of yet) since in the end only file 4 remains since it rewrites the previous ones.

有没有办法加载所有这些文件并将它们存储在结构中的不同变量中(仅针对一个测试集)?因为手动完成这一切是很多工作。

Is there a way to load all these files and store them in different variables in a structure (regarding only one test set)? Because doing all this manually is a hell of a lot of work.

我试图使用这种方法:将多个.mat文件加载到Matlab工作区但我收到无效字段名称加载时出错。(char(文件))=加载(文件);

I've tried to use this method: Load Multiple .mat Files to Matlab workspace but I get an Invalid field name error on loaded.(char(file)) = load(file);

推荐答案

你可以加载到一个变量(最好是一个单元格数组)

You can load into a variable (preferably a cell array)

results = cell( 2, 4 ); % allocate
for testi=1:2
    for resi = 1:4
        filename = sprintf('results_test%d_%d.mat', testi, resi );
        results{testi,resi} = load( filename );
    end
end

现在您将所有结果存储在结果单元格数组,您可以访问存储的变量,例如,

Now you have all the results stored in results cell array and you may access the stored variables, e.g.,

results{1,3}.someVar % access variable someVar (assuming such variable was saves to the corresponding mat file

这篇关于加载多个.mat文件进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆