- "dir": https://www.mathworks.com/help/releases/R2021a/matlab/ref/dir.html
- "fullfile": https://www.mathworks.com/help/releases/R2021a/matlab/ref/fullfile.html
Read .tsv files in different subfolders
7 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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!
0 Comments
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!