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

2 views (last 30 days)
Nar Gomboo
Nar Gomboo on 13 Oct 2021
Commented: Nar Gomboo on 18 Oct 2021
I have 365 output data (.txt files) for one year. Output has a date in its name and each has a structure as follows:
#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 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);
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);
data{i} = thisdata{2} ; % second column
After you can convert them into a matrix if each cell has same data using cell2mat. Read about it.
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)

Community Treasure Hunt

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

Start Hunting!