Store .txt files from different subfolders

1 回表示 (過去 30 日間)
ignacio bobadilla tapia
ignacio bobadilla tapia 2021 年 5 月 20 日
回答済み: Mohammad Sami 2021 年 5 月 20 日
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 Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by