How do I import and read multiple text files with the same name from different folders
8 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm a beginner with matlab and struggling with opening and importing a large set of data. I have a folder DATA with subfolders P01, P02, P03 etc, in each subfolder 10 text files and i would like to open and read 1 txtfile from each subfolder named RESULTS_TRIALS. The data is 19 by 7 and i have 150 participants (P01 - P150), so would like to make a data array of 19x7x150.
I want to make a for loop to direct to all those different folders and importing 1 txt file per subfolder, however I have been struggling for a couple of days now and still dont have any clue how to make this happen.
Thanks in advance
4 个评论
Guillaume
2019-11-27
Oh! My answer will need modification for this type of text file as I assumed from the fact that you want a 3D array that all your data was numerical.
This sort of data should be stored in a table, which can't be 3D. In any case, you would be better off vertically concatenating all the results into just one table.Does the "ID" match the "Pxx" folder name? This would make the whole concatenation trivial, otherwise, I'd just add one variable to the table which is the source folder (assuming that you care about that information).
采纳的回答
Guillaume
2019-11-27
datafolder = 'C:\somewhere\somefolder'; %your root data folder
dataname = 'RESULTS_TRIALS.txt'; %replace with appropriate extension since you haven't specified
filelist = dir(fullfile(datafolder, 'P*', 'dataname')); %get list of dataname files in all P* subfolders of datafolder
%optional: sort filelist by the P folder number:
[~, order] = sort(str2double(regexp({filelist.folder}, '\d+$', 'match', 'once')))
filelist = filelist(order);
%loop over files and import into alldata
alldata = [];
datasize = [];
for fidx = 1:numel(filelist);
filedata = readmatrix(fullfile(filelist(fidx).folder, filelist(fidx).name));
if fidx == 1 %first file read. Preallocate destination matrix
datasize = size(filedata);
alldata = zeros([datasize, numel(filelist)]); %preallocate 3D array
else %2nd to last file, make sure the size match previous files
assert(size(filedata) == datasize, 'Inconsistent matrix size in folder %s', filelist(fidx).folder);
end
alldata(:, :, fidx) = filedata; %copy file data in destination matrix
end
5 个评论
Guillaume
2019-11-27
Yep, sorry it was meant to be f.name indeed. Fixed now.
Assuming that by first row, you mean the Trial index 0:
importedtables{3}(importedtables{3}.Trial_Index_ == 0, :) = []; %delete all rows whose trial index is 0
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!