Store .txt files from different subfolders

1 次查看(过去 30 天)
How to load .txt data from different subfolders and store them in a hyper matrix using a for loop, there are 19 subfolders (subfolder 1 ... 19), and each one contains 7 .txt files of my interest with different names that go from (pt1 ... pt7.) Thank you in advance.

回答(1 个)

Mohammad Sami
Mohammad Sami 2021-5-20
You can use the dir function to list all the txt files in the folder and its subfolders.
mytopleveldir = "C:\path\to\my\toplevel\dir";
alltxtfiles = dir(fullfile(mytopleveldir,'**','*.txt'));
pat = {'pt1' 'pt2' 'pt3'}; %etc
filter = startsWith({alltxtfiles.name},pat); %use appropriate filter to get files of interest
alltxtfiles = alltxtfiles(filter);
alldata = cell(length(alltxtfiles),1);
for i = 1:lenght(alltxtfiles)
% use your import function or readtable
alldata{i} = readtable(fullfile(alltxtfiles(i).folder,alltxtfiles(i).name));
end
% concatentate data based on your data structure
alldata = vertcat(alldata{:});

类别

Help CenterFile Exchange 中查找有关 Manage Products 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by