Write txt to table with collumn name

조회 수: 3 (최근 30일)
Sarah Zven
Sarah Zven 2022년 3월 1일
답변: Peter Perkins 2022년 3월 2일
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