Store .txt files from different subfolders

1 Ansicht (letzte 30 Tage)
ignacio bobadilla tapia
ignacio bobadilla tapia am 20 Mai 2021
Beantwortet: Mohammad Sami am 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.

Antworten (1)

Mohammad Sami
Mohammad Sami am 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{:});

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by