Store .txt files from different subfolders

1 visualización (últimos 30 días)
ignacio bobadilla tapia
ignacio bobadilla tapia el 20 de Mayo de 2021
Respondida: Mohammad Sami el 20 de Mayo de 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.

Respuestas (1)

Mohammad Sami
Mohammad Sami el 20 de Mayo de 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{:});

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by