Reading and combining multiple .txt files
22 views (last 30 days)
Show older comments
Hi everyone
I have received a number of data files related to ship speed, each file is for 1 day with readings taken at 1 minute intervals. I would like to combine these files to create one data set covering a period of operations, before analysing this for time spent in different speed ranges over the time period.
My first issue is how to combine the files. Each .txt file contains a header on the first row, so I would like to eliminate this from the data set. The files are also named e.g. 01-Nov-03, so are not in a purely numerical order. I would like to eventually be able to combine n number of files, so I can extend the evaluation period as required.
I studied Matlab at university but am a little rusty now, so any help is greatly appreciated :)
Best regards, Henry
4 Comments
Answers (1)
Mathieu NOE
on 28 Mar 2022
hello again
try this - we can expand on that for further processing or saving...
natsortfiles is a very usefull tool to make sure files are sorted in natural order (what matlab is not good at)

clc
clearvars
% read current filenames in folder
S = dir('**/*.txt');
S = natsortfiles(S); % natsortfiles now works on the entire structure
% natsortfiles available from FEX :
% https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort?s_tid=srchtitle
figure(1),hold on
for k = 1:numel(S)
S(k).name % display file name in command window (for info only, you can remove / comment this line)
F = fullfile(S(k).folder, S(k).name);
%S(k).data = load(F); % or READTABLE or whatever.
out = readmatrix(F ,"NumHeaderLines", 1);
S(k).data = out(:,13); % this store the 13th column
% plot (for fun)
legstr{k} = S(k).name; % legend string
plot(S(k).data);
end
legend(legstr);
% % Take a look in the structure S: it contains all of your file data and the corresponding filenames, just as you require.
% % For example, the 2nd filename and its data:
% S(2).name
% S(2).data
3 Comments
Mathieu NOE
on 28 Mar 2022
hi again
you can simply remove or comment the line with natsortfiles for the time being
in the future , you may want to download that usefull function from the File Exchange
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!