Writing mutiple data files in a single text file

3 views (last 30 days)
Hi:
I have more than 50 text files named as: "w48_0.txt". Each text file contains two columns of variables. first row in each file is text header.
I want to combine all 50 files in a single text file such that the first column in new file remains the same while second column of every file writes according to the file number 1,2,3...50.
After that I want to plot these data.
How can I check the data written in new text file is correct?
All the files are in the following format:
frequency pressure
3 12
5 10
10 20
. .
.
I want to keep the first column same in new file but want to add second column in new file from all the files.
Can anyone help me?
Haider

Answers (3)

Friedrich
Friedrich on 12 Jul 2011
Hi,
I would read in all the files, e.g
fid = fopen('w48_0.txt','r');
C = textscan(fid, '%f %f','Headerlines',1)
C{1} is the frequency column and C{2} is the pressure. Collect them all from the differnt files and then write them back and plot them.
To get all files easily you can do:
folder_content = dir('C:\folder_with_files');
for i=1:numel(folder_content)
%is non folder, so its a file
if ~folder_content(i).isdir
fid = fopen(fullfile('C:\folder_with_files',a.name),'r');
C = textscan(fid, '%f %f','Headerlines',1);
%do with the data whatever you like
fclose(fid);
end
end

Andrei Bobrov
Andrei Bobrov on 12 Jul 2011
out = [];
for i1 = 1:50
fid = fopen(['w' num2str(i1) '_0.txt'],'r');
C = textscan(fid,'%f %f','Headerlines',1);
out=[out;cell2mat(C)]
fclose(fid);
end
dlmwrite('newfile.txt', out,'delimiter', ' ','newline', 'pc')

Ali Haider
Ali Haider on 13 Jul 2011
I have 50 files. Now I am demostrating two:
file1 named : w1_0.dat it contains data in the format as:
F G1
61.035156 6.871723767351
64.849854 6.565941813237
68.664551 6.942959265754
72.479248 6.656110691291
76.293945 7.028008414744
80.108643 7.559239901553
83.92334 8.336993423748
87.738037 9.509469830548
91.552734 14.514696834071
file2 named : w2_0.dat it contains data in the format as:
F G2
61.035156 6.871723767351
64.849854 6.565941813237
68.664551 6.942959265754
72.479248 6.656110691291
76.293945 7.028008414744
80.108643 7.559239901553
83.92334 8.336993423748
87.738037 9.509469830548
91.552734 14.514696834071
I want to combine both these files in a new *.dat files such that:
F filename1 filename2
61.035156 6.871723767 6.871723767
64.849854 6.565941813 6.565941813
68.664551 6.942959266 6.942959266
72.479248 6.656110691 6.656110691
76.293945 7.028008415 7.028008415
80.108643 7.559239902 7.559239902
83.92334 8.336993424 8.336993424
87.738037 9.509469831 9.509469831
91.552734 14.51469683 14.51469683
F remains the same wile column 2 of every *.dat file added in the newly created dat file. first row will be the variable names. I can arrange all the dat in the said format. Finally plot the data such that F vs. all the Gs.

Categories

Find more on Data Import and Export 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!