Write txt to table with collumn name

3 次查看(过去 30 天)
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

回答(2 个)

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

Peter Perkins
Peter Perkins 2022-3-2
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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by