How do I copy column datas from multiple .txt file to new file?

3 views (last 30 days)
I have 365 output data (.txt files) for one year. Output has a date in its name and each has a structure as follows:
#comment
#comment
#comment
#comment
#comment
#Time(UTC) nV
20140102T000000 006.36
20140102T000030 006.39
20140102T000100 006.43
20140102T000130 006.46
20140102T000200 006.49
20140102T000230 006.53
20140102T000300 006.56
20140102T000330 006.59
20140102T000400 006.60
20140102T000430 006.63
20140102T000500 006.67
.
.
.
.
I attached an example file. It is a data of 2nd Jan, 2014.
What I want to do is to copy value of nV with the date from all files to new one combined result file.
For one file I tried readtable function to plot for one day. However, I faces problems to plot for one year data.

Accepted Answer

KSSV
KSSV on 13 Oct 2021
You can read your text file using the below code snippet.
filename = 'ab20140102.txt' ;
fid = fopen(filename,'r');
data = textscan(fid,'%s%f','HeaderLines',6);
fclose(fid);
c2 = data{2} ; % second column
To read all the text files and save them use:
files = dir('*.txt') ;
N = length(files); % total number of files
data = cell(N,1) ; % store data into cell
% loop for each file
for i = 1:N
thisfile = files(i).name ;
fid = fopen(thisfile,'r');
thisdata = textscan(fid,'%s%f','HeaderLines',6);
fclose(fid);
data{i} = thisdata{2} ; % second column
end
After you can convert them into a matrix if each cell has same data using cell2mat. Read about it.
  3 Comments
Nar Gomboo
Nar Gomboo on 18 Oct 2021
I had skipped the cell2mat part. Now It is working perfectly. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!