Help with the convertion of text files into tables.

26 ビュー (過去 30 日間)
Jonathan
Jonathan 2024 年 12 月 7 日 13:05
回答済み: Walter Roberson 2024 年 12 月 7 日 22:23
Hi, I have a file containing 18 text files. I managed to put these in a structure so I can easely acces these files from there later on, when they are not in my path. Now, I am strugeling with pulling files from that structure and making the text files into tables. The text files contain 5000 rows and 2 columns. Can someone help me?
  1 件のコメント
dpb
dpb 2024 年 12 月 7 日 14:58
What's wrong with readtable directly from the file(s)? Use fullfile and dir to process the list, wherever the files reside...

サインインしてコメントする。

回答 (2 件)

Anjaneyulu Bairi
Anjaneyulu Bairi 2024 年 12 月 7 日 16:49
To convert files from your structure into tables, start by accessing the structure fields to obtain the file paths. Use the "fullfile" function to construct the full file path, and then pass this path to the "readtable" function to create a table by reading the column-oriented data from your files.
To know more about "readtable" and "fullfile" functions, visit the below documentation links:

Walter Roberson
Walter Roberson 2024 年 12 月 7 日 22:23
directory_files_are_in = '/path/to/where/the/files/are';
dinfo = dir( fullfile(directory_files_are_in, '*.txt')); %adjust .txt as needed
fullnames = fullfile({dinfo.folder}, {dinfo.name});
num_files = length(fullnames);
all_data = cell(num_files,1);
for K = 1 : num_files
this_file = fullnames{K};
all_data{K} = readtable(this_file);
end
The accumulated data will now be stored in all_data as a cell array, with each cell array being a table.

Community Treasure Hunt

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

Start Hunting!

Translated by