Clear Filters
Clear Filters

Write txt to table with collumn name

1 view (last 30 days)
Sarah Zven
Sarah Zven on 1 Mar 2022
Answered: Peter Perkins on 2 Mar 2022
Hello,
I need to go through 100 folders -> take txt file (name of the file is the same for all folders) and write it to table. The txt file contains only one collum with value I need to import to table for all folder, so one folder one collum, but only values greater than 0. I'm wondering, is there a way that the name of collum would be name of folder from which I import files values? I would appreciate if someone could take the time to help me, I'm new to MATLAB. Thank you

Answers (2)

Walter Roberson
Walter Roberson on 1 Mar 2022
https://www.mathworks.com/help/matlab/ref/table.addvars.html

Peter Perkins
Peter Perkins on 2 Mar 2022
Sarah, you question is a little vague. It sounds like you have a series of folders, let's say they are named folder1, folder2, ... folder100. And in each one there's a text file named data.txt, and every data.txt contains one column of numbers. If that's not what you have then please be more specific and provide a short, clear example.
It sounds like you need to write a loop over the folders, perhaps using dir, then read data.txt using readtable or readmatrix, then combine all those results. I would recommend readmatrix if you have a recent enough MATLAB, and then at each iteration of the loop, you would do something like
t = table;
for i = 1:length(folders)
foldername = folders{i}; % assuming you have the folder names in a cell array of char vectors
x = readmatrix('data.txt', ...);
t.(foldername) = x;
end

Categories

Find more on Environment and Settings 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!