提问人:user1042343 提问时间:12/5/2013 最后编辑:user1042343 更新时间:12/5/2013 访问量:70
遍历 matfile - Matlab
looping over matfiles - Matlab
问:
我有一系列名为 m1 的 matfile...M38型。我需要能够一次访问所有这些,所以我使用以下命令:
fileList=dir('cleanSample*');
m1=matfile(fileList(1).name);.....
我硬编码了所有 matfile 语句。
然后,我需要遍历所有这些文件,并从其中包含的矩阵中提取特定行:
for i=1:num1
arrWrite=m1.outputArray(i,:);
for j=2:num2
thename=sprintf('m%i',j);
addArray=thename.outputArray(i,:);
但是在最后一行,我收到错误:“尝试引用非结构数组的字段。有没有办法在不遍历所有垫子文件的情况下做到这一点?
编辑:它大约有 20 GB 的 matfiles,所以我不能将它们同时存储在内存中。
答:
1赞
Vuwox
12/5/2013
#1
问题是因为不是一个结构。它是一个字符串。thename
thename=sprintf('m%i',j);
所以不是这个字符串的变量。它是 matfile 中的变量。outputArray
1赞
Peter
12/5/2013
#2
将 matfile 对象放入元胞数组中,而不是按顺序命名它们:
for ii=1:length(fileList)
m{ii} = matfile(fileList(ii).name);
end
for i=1:num1
arrWrite = m{1}.outputArray(i,:);
for j=2:num2
addArray = m{j}.outputArray(i,:);
我真的不明白你的索引,但你明白了......
评论
0赞
Vuwox
12/5/2013
如果你看一下编辑,他无法将所有 matfile 加载到内存中。所以他需要一个接一个地打开它们,并改变他所拥有的东西。主要问题不在于循环。因为我做得很好。问题是如何存储回去。
0赞
Peter
12/5/2013
matfile 对象不会将内容加载到内存中:它们只是文件的句柄。
0赞
Strategy Thinker
12/5/2013
#3
这是一个函数,它循环文件并提取一个特定的变量,使用该特定变量,您可以随心所欲地做任何事情。为此,我将这些变量存储在 a 中,因为我不知道您想对这些变量做什么,或者这些变量将是什么类型;A 类似于一个数组,但对其条目的限制较少,它用于访问元素和访问子单元格。cell
cell
{}
()
function TestMFiles
fileNamePrefex = 'm';
numFiles = 38;
%// Pre-Initialize the matrix to save the files in
all2ndEntries = cell(numFiles, 1);
for k = 1:numFiles
%// temporarily store the wanted variables a in variable
currFileName = [fileNamePrefex num2str(k)];
currFile = load(currFileName);
currMat = currFile.Mat;
%// Store the wanted entries in a cell
all2ndEntries{k,1} = currMat(2,1);
end
%// Do with cell want you want to do
disp(all2ndEntries);
end
如果内存有问题,那么你可能希望在文件的某个位置放置一个命令,这调用了垃圾回收器等。虽然它相当慢。pack
上一个:MPI 发送/接收错误
评论