How to Load Multiple Text Files Without Sequential Names
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I'm new to Matlab and I would dearly need your help!
I would like to load 30 text files, so each one can be red as a matrix and that I can make operations on them (loops, averaging, etc.), but I haven't found a way to load them all at once. Unfortunately, the spectrometer that I use create files like this :
1903395U1_04june19_154040_0001.Raw8.txt : which means "Measurements taken on June 4th, 2019 at 15 hours 40 min 40 seconds, take 0001"
1903395U1_04june19_154040_0002.Raw8.txt : which means "Measurements taken on June 4th, 2019 at 15 hours 40 min 40 seconds, take 0002"
1903395U1_04june19_154040_0003.Raw8.txt : which means "Measurements taken on June 4th, 2019 at 15 hours 40 min 40 seconds, take 0003"
1903395U1_04june19_154040_0004.Raw8.txt : which means "Measurements taken on June 4th, 2019 at 15 hours 40 min 40 seconds, take 0004"
1903395U1_04june19_154041_0001.Raw8.txt : which means "Measurements taken on June 4th, 2019 at 15 hours 40 min 41 seconds, take 0001"
My objective is to stock all "Columns 2" (only the values) of these text files into a pre-allocated matrix (i.e. Column 2 of the first file, Column 2 of the second file, Column 2 of the third file and so on...).
Column 2 (values of the first file) Column 2 (values of the second file) Column 2 (values of the third file) ........
1 2 3 .......
3 7 1 .......
... ... ... .......
Then, I need to average each row and stock the averaged value in another pre-allocated matrix and for example :
1 2 3 ....
3 7 1 ...
... ... ... ...
Averaging each row would give something like this :
2
3.66666666
...
What would be the best way to treat this problem in Matlab.
Thank you,
have a great day!
댓글 수: 0
채택된 답변
Josh
2019년 6월 6일
I make a folder that contains only the text files you want to combine. Then you can do something like this to load the file paths into MATLAB:
% Set input folder
input_folder = 'C:\whatever';
% Read all *.txt files from input folder
% NOTE: This creates a MATLAB struct with a bunch of info about each text file
% in the folder you specified.
files = dir(fullfile(input_folder, '*.txt'));
% Get full path names for each text file
file_paths = fullfile({files.folder}, {files.name});
Once you have them, you can read the data using textread:
% Read data from files, keep second column
for i = 1 : numel(file_paths)
% Read data from ith file.
% NOTE: If you're file has a text header, missing data, or
% uses non white-space delimiters, you should check out the
% documentation for textread to determine which options to use.
data = textread(file_paths{i}, '');
% Save second data column to matrix
% NOTE: Your data files all need to have the same number of rows for this to work
A(:, i) = data(:, 2);
end
% Calculate the average of the rows (second dimension) of A:
avg = mean(A, 2);
댓글 수: 2
ANURAG VERMA
2022년 2월 27일
WHAT IS THE MEANING OF: A(:, i) = data(:, 2);
IT GIVES ERROR AND I HAVE TO MAKE 2 AS 1?
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!