访问存储在不同文件夹中的不同文件中的数据 [英] Access data in different files stored in different folders
问题描述
我已经四处搜寻,找到了一些可能解决我问题的方法,但是无法执行代码.
I've searched around and found some potential solutions for my problem but have been unable to implement the code.
基本上,我有一个目录,其中包含 32个子文件夹. 32个子文件夹中的每个子文件夹中都有 4个文件(.mat文件,每行1行,几百万列).我感兴趣的变量称为 data (请参见下面的代码).
Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).
我需要访问子文件夹的子集中的所有4个.mat文件,并将它们附加/连接到一个大矩阵中.此外,每个子文件夹中的每4个文件组应在末尾矩阵中彼此相邻.
I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.
此外,子文件夹的名称和其中的文件也是已知的:
Also, the names of the sub-folders and the files within are known:
文件夹= TT1,TT2,等.
文件= TT1ch1,TT1ch2,TT1ch3,TT1ch4; TT2ch1,TT2ch2,TT2ch3,TT2ch4等.
Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.
我还需要在代码中指定实际打开和读取4个文件的子文件夹.并非所有时间都需要阅读.到目前为止,我有这个:
I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:
TTs = [1,2,3,4,5]; % List of sub-folders to use.
for i = TTs;
addpath(strcat('TT',num2str(i)));
cd (strcat('TT',num2str(i)));
for w = 1:4; %get data from the 4 files
load(strcat('TT',num2str(i),'ch', num2str(w), '.mat'));
allChs(w,:) = data(1,:); %concatenate into one matrix
end
cd ..
rmpath(strcat('TT',num2str(i)));
end
使用此代码,我可以从给定子文件夹的4个文件中读取数据,并将其复制到新的矩阵(allChs)中.但是,当我尝试添加代码以遍历所有文件夹时,我只会覆盖我所拥有的...
With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...
我尝试了不同的方法,但是在这个阶段还很困难.任何帮助都将受到欢迎.
I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.
干杯, 大子
推荐答案
正如@Cris Luengo所说,您无需将路径添加到文件夹中即可读取.
As @Cris Luengo said, you don't need add to path a folder for reading from it.
此外,您不需要cd
,最好明确显示要从中读取的路径:
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
现在,您需要做的是继续移动文件夹,而不会在下一个子文件夹中覆盖它.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
这篇关于访问存储在不同文件夹中的不同文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!