Read .tsv files in different subfolders

7 views (last 30 days)
Mário Sobrinho
Mário Sobrinho on 11 Jun 2021
Answered: Arjun on 27 Feb 2025
Hello,
How can I read at once all the .tsv files inside different subfolders? Imagine that I have 100 files in 10 subfolders each. I would like to read all the data importing to one matrix.
If the files are stored in one folder I use the following code:
ds = tabularTextDatastore(filename,'FileExtensions','.tsv');
T = readall(ds);
data=table2array(T);
with the filename linked to one folder. In case of multiple subfolder I don't know how to do it.
Thanks

Answers (1)

Arjun
Arjun on 27 Feb 2025
I see that you want to read data from multiple ".TSV" files, which are organized in subfolders where each subfolder contains 10 files, and in all, you have 10 such subfolders, which makes a total of 100 ".TSV" files.
In order to do so, you can use the "dir" function of MATLAB to get a list of all the files ending with the extension ".TSV" inside a main folder.
% parent folder is the main folder which contains these subfolders
files = dir(fullfile(parentFolder, '**', '*.tsv'));
Once you have all the files, organize them in a cell array with complete path using the "fullfile" function:
% cell array with paths to .tsv files
fileList = fullfile({files.folder}, {files.name});
Finally, you can now collect data from all the files and store them in a container.
% Define empty container
allData = [];
% Iterate and concatenate data
for i = 1:length(fileList)
ds = tabularTextDatastore(fileList{i}, 'FileExtensions', '.tsv');
T = readall(ds);
data = table2array(T);
allData = [allData; data];
end
% Finally display
disp(allData);
Kindly refer to the documentaion of the following functions used above:
I hope this helps!

Community Treasure Hunt

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

Start Hunting!