How to Load Multiple Text Files Without Sequential Names

10 views (last 30 days)
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!

Accepted Answer

Josh
Josh on 6 Jun 2019
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 Comments
ANURAG VERMA
ANURAG VERMA on 27 Feb 2022
WHAT IS THE MEANING OF: A(:, i) = data(:, 2);
IT GIVES ERROR AND I HAVE TO MAKE 2 AS 1?

Sign in to comment.

More Answers (1)

Ragda2
Ragda2 on 24 Jan 2020
i couldn't get the files paths can anyone know why?

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!