Store .txt files from different subfolders

1 vue (au cours des 30 derniers jours)
ignacio bobadilla tapia
ignacio bobadilla tapia le 20 Mai 2021
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.

Réponses (1)

Mohammad Sami
Mohammad Sami le 20 Mai 2021
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{:});

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by